Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the stop spectating entity rule type #311

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public final class GameRuleType {
.enforces(CoralDeathEvent.EVENT, result -> (world, pos, from, to) -> result);

public static final GameRuleType DISMOUNT_VEHICLE = GameRuleType.create();
public static final GameRuleType STOP_SPECTATING_ENTITY = GameRuleType.create();
public static final GameRuleType PLAYER_PROJECTILE_KNOCKBACK = GameRuleType.create();
public static final GameRuleType TRIDENTS_LOYAL_IN_VOID = GameRuleType.create();
public static final GameRuleType MODIFY_INVENTORY = GameRuleType.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xyz.nucleoid.plasmid.mixin.game.rule;

import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import com.mojang.authlib.GameProfile;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
Expand All @@ -9,14 +11,32 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import xyz.nucleoid.plasmid.api.game.rule.GameRuleType;
import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl;
import xyz.nucleoid.stimuli.event.EventResult;

@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntityMixin extends PlayerEntity {
private ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile profile) {
super(world, pos, yaw, profile);
}

@WrapWithCondition(
method = "tick",
// The targeted call handles shifting to stop spectating
// The other call handles dead entities, which should always stop being spectated
at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;setCameraEntity(Lnet/minecraft/entity/Entity;)V", ordinal = 0)
)
private boolean preventStopSpectatingEntity(ServerPlayerEntity player, Entity entity) {
var gameSpace = GameSpaceManagerImpl.get().byPlayer(player);

if (gameSpace != null && gameSpace.getBehavior().testRule(GameRuleType.STOP_SPECTATING_ENTITY) == EventResult.DENY) {
return false;
}

return true;
}

@Inject(method = "isPvpEnabled", at = @At("HEAD"), cancellable = true)
private void allowPvPInGames(CallbackInfoReturnable<Boolean> cir) {
var gameSpace = GameSpaceManagerImpl.get().byPlayer(this);
Expand Down
13 changes: 12 additions & 1 deletion src/testmod/java/xyz/nucleoid/plasmid/test/TestGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import net.minecraft.block.ButtonBlock;
import net.minecraft.block.LeavesBlock;
import net.minecraft.block.enums.BlockFace;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.scoreboard.AbstractTeam;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -18,11 +20,13 @@
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameMode;
import net.minecraft.world.GameRules;
import xyz.nucleoid.fantasy.RuntimeWorldConfig;
import xyz.nucleoid.map_templates.BlockBounds;
import xyz.nucleoid.map_templates.MapEntity;
import xyz.nucleoid.map_templates.MapTemplate;
import xyz.nucleoid.plasmid.api.game.*;
import xyz.nucleoid.plasmid.api.game.common.team.*;
Expand Down Expand Up @@ -152,7 +156,7 @@ private static GameResult startGame(GameSpace gameSpace) {
long currentTime = gameSpace.getTime();
activity.deny(GameRuleType.PVP).allow(GameRuleType.MODIFY_ARMOR);
activity.deny(GameRuleType.FALL_DAMAGE).deny(GameRuleType.HUNGER);
activity.deny(GameRuleType.THROW_ITEMS);
activity.deny(GameRuleType.THROW_ITEMS).deny(GameRuleType.STOP_SPECTATING_ENTITY);

activity.deny(GameRuleType.INTERACTION).allow(GameRuleType.USE_BLOCKS);

Expand Down Expand Up @@ -214,6 +218,13 @@ private static MapTemplate generateMapTemplate(BlockState state) {
var edge = new BlockPos(max.getX(), max.getY() + 1, max.getZ());
template.setBlockState(edge, BUTTON);

var armorStandNbt = new NbtCompound();
armorStandNbt.putString("id", EntityType.getId(EntityType.ARMOR_STAND).toString());
armorStandNbt.putBoolean("NoGravity", true);

var armorStandPos = Vec3d.ofBottomCenter(edge.offset(Direction.WEST));
template.addEntity(new MapEntity(armorStandPos, armorStandNbt));

for (var pos : bounds) {
template.setBlockState(pos, state);
}
Expand Down
Loading