Skip to content

Commit

Permalink
Fix the villager
Browse files Browse the repository at this point in the history
Signed-off-by: Patbox <[email protected]>
  • Loading branch information
Patbox committed Jul 16, 2023
1 parent d1ceb58 commit fff0972
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 38 deletions.
5 changes: 3 additions & 2 deletions src/main/java/eu/pb4/buildbattle/BuildBattle.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKeys;
Expand Down Expand Up @@ -36,13 +37,13 @@ public class BuildBattle implements ModInitializer {

public static final GameRuleType CREATIVE_LIMIT = GameRuleType.create();

public static final TagKey BANNED_ITEMS = TagKey.of(RegistryKeys.ITEM, new Identifier(ID, "banned"));
public static final TagKey<Item> BANNED_ITEMS = TagKey.of(RegistryKeys.ITEM, new Identifier(ID, "banned"));

@Override
public void onInitialize() {
BBItems.register();
Registry.register(Registries.ENTITY_TYPE, new Identifier(ID, "floor_changer"), FloorChangingEntity.TYPE);
FabricDefaultAttributeRegistry.register(FloorChangingEntity.TYPE, FloorChangingEntity.createMobAttributes());
FabricDefaultAttributeRegistry.register(FloorChangingEntity.TYPE, FloorChangingEntity.createLivingAttributes());
PolymerEntityUtils.registerType(FloorChangingEntity.TYPE);

ThemesRegistry.register();
Expand Down
72 changes: 48 additions & 24 deletions src/main/java/eu/pb4/buildbattle/custom/FloorChangingEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.datafixers.util.Pair;
import eu.pb4.buildbattle.game.map.BuildArena;
import eu.pb4.buildbattle.game.stages.BuildingStage;
import eu.pb4.buildbattle.mixin.VillagerEntityAccessor;
import eu.pb4.buildbattle.other.BbUtils;
import eu.pb4.polymer.core.api.entity.PolymerEntity;
Expand All @@ -17,24 +18,24 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Arm;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.village.VillagerData;
import net.minecraft.world.World;
import xyz.nucleoid.plasmid.game.manager.GameSpaceManager;

import java.util.List;

public class FloorChangingEntity extends MobEntity implements PolymerEntity {
public class FloorChangingEntity extends LivingEntity implements PolymerEntity {
public static EntityType<FloorChangingEntity> TYPE = FabricEntityTypeBuilder.<FloorChangingEntity>create(SpawnGroup.MISC, FloorChangingEntity::new).dimensions(EntityDimensions.fixed(0.75f, 2f)).build();
private BuildArena buildArena;
private final VillagerData villagerData;
private ItemStack lastUsedFloor = Items.GRASS_BLOCK.getDefaultStack();

public FloorChangingEntity(EntityType<FloorChangingEntity> type, World world) {
super(type, world);
this.buildArena = null;
this.setPersistent();
this.setCustomNameVisible(true);
this.setSilent(true);
this.setNoGravity(true);
Expand All @@ -46,11 +47,6 @@ public FloorChangingEntity(World world) {
this(TYPE, world);
}

public FloorChangingEntity(World world, BuildArena arena) {
this(world);
this.buildArena = arena;
}

@Override
public void tick() {
super.tick();
Expand All @@ -61,18 +57,30 @@ public void tickMovement() {
this.turnHead(this.getYaw(), this.getYaw());
}

@Override
public Arm getMainArm() {
return Arm.RIGHT;
}

@Override
public boolean canTakeDamage() {
return false;
}

@Override
public boolean canBeLeashedBy(PlayerEntity player) {
return false;
public Iterable<ItemStack> getArmorItems() {
return List.of();
}

@Override
public void attachLeash(Entity entity, boolean sendPacket) { }
public ItemStack getEquippedStack(EquipmentSlot slot) {
return ItemStack.EMPTY;
}

@Override
public void equipStack(EquipmentSlot slot, ItemStack stack) {

}

@Override
public EntityType<?> getPolymerEntityType(ServerPlayerEntity player) {
Expand All @@ -89,22 +97,38 @@ public void modifyRawTrackedData(List<DataTracker.SerializedEntry<?>> data, Serv
data.add(DataTracker.SerializedEntry.of(VillagerEntityAccessor.get(), this.villagerData));
}


@Override
public ActionResult interactMob(PlayerEntity player, Hand hand) {
if (this.buildArena != null) {
if (this.buildArena.isBuilder(player)) {
BlockState state = BbUtils.getStateFrom((ServerPlayerEntity) player, player.getStackInHand(hand));
if (state != null) {
this.lastUsedFloor = player.getStackInHand(hand);
this.equipStack(EquipmentSlot.MAINHAND, this.lastUsedFloor);
for (BlockPos blockPos : this.buildArena.ground) {
this.getWorld().setBlockState(blockPos, state);
}

return ActionResult.SUCCESS;
public ActionResult interact(PlayerEntity player, Hand hand) {
BuildArena buildArena = null;
var game = GameSpaceManager.get().byWorld(this.getWorld());
if (game != null) {
BuildingStage stage = game.getAttachment("game_active");

if (stage != null) {
buildArena = stage.gameMap.getArena(this.getBlockPos());
}
}


if (buildArena == null) {
return ActionResult.FAIL;
}


if (buildArena.isBuilder(player)) {
BlockState state = BbUtils.getStateFrom((ServerPlayerEntity) player, player.getStackInHand(hand));
if (state != null) {
this.lastUsedFloor = player.getStackInHand(hand);
this.equipStack(EquipmentSlot.MAINHAND, this.lastUsedFloor);
for (BlockPos blockPos : buildArena.ground) {
this.getWorld().setBlockState(blockPos, state);
}

return ActionResult.SUCCESS;
}
}

return ActionResult.FAIL;
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/eu/pb4/buildbattle/game/map/BuildArena.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class BuildArena {
public final Set<UUID> playersUuid = new HashSet<>();
private final Vec3d entityPos;
public int score = 0;
private FloorChangingEntity entity = null;
private UUID entityUuid;

public BuildArena(BlockBounds area, BlockBounds ground, BlockBounds bounds, BlockBounds spawn, Vec3d entityPos) {
this.buildingArea = area;
Expand Down Expand Up @@ -74,18 +74,22 @@ public void addPlayer(PlayerData data) {
}

public void spawnEntity(ServerWorld world, float yaw) {
FloorChangingEntity entity = new FloorChangingEntity(world, this);
entity.setPos(this.entityPos.x, this.entityPos.y, this.entityPos.z);
FloorChangingEntity entity = new FloorChangingEntity(world);
entity.setPosition(this.entityPos.x, this.entityPos.y, this.entityPos.z);
world.spawnEntity(entity);
entity.setYaw(yaw);
entity.setBodyYaw(yaw);
entity.setHeadYaw(yaw);
this.entity = entity;
this.entityUuid = entity.getUuid();
}

public void removeEntity() {
if (this.entity != null) {
this.entity.remove(Entity.RemovalReason.DISCARDED);
public void removeEntity(ServerWorld world) {
if (this.entityUuid != null) {
var entity = world.getEntity(this.entityUuid);

if (entity != null) {
entity.remove(Entity.RemovalReason.DISCARDED);
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/main/java/eu/pb4/buildbattle/game/stages/BuildingStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ private BuildingStage(GameSpace gameSpace, ServerWorld world, GameplayMap map, G
for (BuildArena buildArena : this.gameMap.buildArena) {
buildArena.spawnEntity(world, this.config.mapConfig().entityRotation());
}
gameSpace.setAttachment("game_active", this);
}

public static void open(GameSpace gameSpace, BuildBattleConfig config, Runnable runAfterTeleporting) {
Expand Down Expand Up @@ -186,8 +187,6 @@ public static void open(GameSpace gameSpace, BuildBattleConfig config, Runnable
game.listen(BlockUseEvent.EVENT, active::onBlockUse);
game.listen(BuildBattle.ON_BUCKET_USAGE, active::onFluidPlace);

game.listen(ItemUseEvent.EVENT, active::onItemUse);

game.listen(PlayerAttackEntityEvent.EVENT, active::onEntityDamage);
game.listen(GameActivityEvents.TICK, active::tick);
game.listen(ExplosionDetonatedEvent.EVENT, active::onExplosion);
Expand Down Expand Up @@ -431,7 +430,9 @@ private void onOpen() {
if (this.gameSpace.getPlayers().contains(ref)) {
ref.ifOnline(world, (p) -> {
this.spawnParticipant(p);
this.themeVotingManager.addPlayer(p);
if (this.themeVotingManager != null) {
this.themeVotingManager.addPlayer(p);
}
});

}
Expand Down Expand Up @@ -491,8 +492,10 @@ private void tick() {
this.lockBuilding = true;
this.timerBar.setColor(BossBar.Color.RED);
this.timerBar.update(Text.translatable("text.buildbattle.timer_bar.times_up"), 0);
gameSpace.setAttachment("game_active", null);

for (BuildArena buildArena : this.gameMap.buildArena) {
buildArena.removeEntity();
buildArena.removeEntity(world);
}
this.phase = Phase.WAITING;
this.gameSpace.getPlayers().playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.PLAYERS, 1.0f, 1.5f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public static GameOpenProcedure open(GameOpenContext<BuildBattleConfig> context)
.setGenerator(waitingMap.asGenerator())
.setGameRule(GameRules.DO_WEATHER_CYCLE, false);


return context.openWithWorld(worldConfig, (game, world) -> {
WaitingStage waiting = new WaitingStage(game.getGameSpace(), waitingMap, config, world);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/data/buildbattle/games/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"entity_rotation": -45
},
"theme": "buildbattle:normal",
"theme_voting": false,
"players": {
"min": 1,
"max": 24
Expand Down

0 comments on commit fff0972

Please sign in to comment.