diff --git a/src/main/java/com/pancake/surviving_the_aftermath/api/PortalShapeAccessor.java b/src/main/java/com/pancake/surviving_the_aftermath/api/PortalShapeAccessor.java new file mode 100644 index 0000000..97acb59 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/api/PortalShapeAccessor.java @@ -0,0 +1,18 @@ +package com.pancake.surviving_the_aftermath.api; + +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; + +public interface PortalShapeAccessor { + //bottomLeft + BlockPos survivingTheAftermath$getBottomLeft(); + + //height + int survivingTheAftermath$getHeight(); + + //width + int survivingTheAftermath$getWidth(); + + //rightDir + Direction survivingTheAftermath$getRightDir(); +} diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/ForgeEventSubscriber.java b/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/ForgeEventSubscriber.java index c4ddc93..742319c 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/ForgeEventSubscriber.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/ForgeEventSubscriber.java @@ -3,18 +3,9 @@ import com.pancake.surviving_the_aftermath.SurvivingTheAftermath; import com.pancake.surviving_the_aftermath.api.aftermath.AftermathManager; import com.pancake.surviving_the_aftermath.common.capability.AftermathCap; -import com.pancake.surviving_the_aftermath.common.init.ModItems; import com.pancake.surviving_the_aftermath.common.init.ModTags; -import com.pancake.surviving_the_aftermath.common.init.ModVillagers; -import com.pancake.surviving_the_aftermath.common.util.RegistryUtil; -import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; -import net.minecraft.world.entity.npc.VillagerProfession; -import net.minecraft.world.entity.npc.VillagerTrades; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.level.levelgen.Heightmap; import net.minecraft.world.level.storage.ServerLevelData; diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/RaidEventSubscriber.java b/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/RaidEventSubscriber.java index 6eb6077..3b06b4e 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/RaidEventSubscriber.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/RaidEventSubscriber.java @@ -5,6 +5,7 @@ import com.pancake.surviving_the_aftermath.common.init.ModSoundEvents; import com.pancake.surviving_the_aftermath.common.init.ModStructures; import com.pancake.surviving_the_aftermath.common.raid.NetherRaid; +import com.pancake.surviving_the_aftermath.common.raid.api.BaseRaid; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.registries.Registries; @@ -16,6 +17,7 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.portal.PortalShape; +import net.minecraftforge.event.entity.EntityJoinLevelEvent; import net.minecraftforge.event.entity.EntityTravelToDimensionEvent; import net.minecraftforge.event.level.BlockEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; @@ -75,7 +77,16 @@ public static void onRaidVictory(AftermathEvent.Victory event) { ModSoundEvents.ORCHELIAS_VOX.get(), SoundSource.NEUTRAL, 3.0F, 1.0F, level.random.nextLong()); } }); + } + @SubscribeEvent + public static void joinRaid(EntityJoinLevelEvent event) { + AftermathManager.getInstance().getAftermathMap().values().stream() + .filter(aftermath -> aftermath instanceof BaseRaid) + .map(aftermath -> (BaseRaid) aftermath) + .forEach(raid -> raid.join(event.getEntity())); } + + } diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/VillagerSubscriber.java b/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/VillagerSubscriber.java index b429a51..10675a4 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/VillagerSubscriber.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/event/subscriber/VillagerSubscriber.java @@ -31,18 +31,18 @@ public class VillagerSubscriber { public static void addCustomTrades(VillagerTradesEvent event) { final ItemStack emerald = new ItemStack(Items.EMERALD, 2); final ItemStack diamond = new ItemStack(Items.DIAMOND, 1); + Int2ObjectMap> trades1 = event.getTrades(); if (event.getType() == ModVillagers.RELIC_DEALER.get()) { - Int2ObjectMap> trades = event.getTrades(); - trades.get(1).add((trader, random) -> enchantBookForNetherCore(random)); + trades1.get(1).add((trader, random) -> enchantBookForNetherCore(random)); } if (event.getType() == VillagerProfession.BUTCHER) { - Int2ObjectMap> trades = event.getTrades(); + Int2ObjectMap> trades = trades1; ItemStack rawFalukorv = new ItemStack(ModItems.RAW_FALUKORV.get(), 1); trades.get(4).add((trader, random) -> newOffer(emerald, rawFalukorv)); trades.get(5).add((trader, random) -> newOffer(diamond, rawFalukorv)); } if (event.getType() == VillagerProfession.FARMER) { - Int2ObjectMap> trades = event.getTrades(); + Int2ObjectMap> trades = trades1; for (Item item : RegistryUtil.getKnownItems()) { if (item.isEdible()) { ItemStack foodStack = item.getDefaultInstance(); diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/mixin/PortalShapeMixin.java b/src/main/java/com/pancake/surviving_the_aftermath/common/mixin/PortalShapeMixin.java new file mode 100644 index 0000000..7118349 --- /dev/null +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/mixin/PortalShapeMixin.java @@ -0,0 +1,53 @@ +package com.pancake.surviving_the_aftermath.common.mixin; + +import com.pancake.surviving_the_aftermath.api.PortalShapeAccessor; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.world.level.portal.PortalShape; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(PortalShape.class) +public class PortalShapeMixin implements PortalShapeAccessor { + //bottomLeft + @Shadow + private BlockPos bottomLeft; + + //height + @Shadow + private int height; + + //width + @Final + @Shadow + private int width; + + //rightDir + @Final + @Shadow + private Direction rightDir; + + + @Override + public BlockPos survivingTheAftermath$getBottomLeft() { + return bottomLeft; + } + + @Override + public int survivingTheAftermath$getHeight() { + return height; + } + + @Override + public int survivingTheAftermath$getWidth() { + return width; + } + + @Override + public Direction survivingTheAftermath$getRightDir() { + return rightDir; + } +} + + diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/NetherRaid.java b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/NetherRaid.java index bdfde82..ba55070 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/NetherRaid.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/NetherRaid.java @@ -1,7 +1,7 @@ package com.pancake.surviving_the_aftermath.common.raid; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import com.pancake.surviving_the_aftermath.SurvivingTheAftermath; import com.pancake.surviving_the_aftermath.api.AftermathState; import com.pancake.surviving_the_aftermath.api.Constant; @@ -10,12 +10,14 @@ import com.pancake.surviving_the_aftermath.api.base.BaseAftermathModule; import com.pancake.surviving_the_aftermath.api.module.IEntityInfoModule; import com.pancake.surviving_the_aftermath.common.init.ModStructures; +import com.pancake.surviving_the_aftermath.api.PortalShapeAccessor; import com.pancake.surviving_the_aftermath.common.raid.api.BaseRaid; import com.pancake.surviving_the_aftermath.common.raid.module.NetherRaidModule; import com.pancake.surviving_the_aftermath.common.structure.NetherRaidStructure; import com.pancake.surviving_the_aftermath.common.tracker.RaidMobBattleTracker; import com.pancake.surviving_the_aftermath.common.tracker.RaidPlayerBattleTracker; import com.pancake.surviving_the_aftermath.common.util.AftermathEventUtil; +import com.pancake.surviving_the_aftermath.common.util.RandomUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.particles.ParticleTypes; @@ -35,6 +37,7 @@ import net.minecraft.world.entity.monster.Slime; import net.minecraft.world.entity.monster.hoglin.Hoglin; import net.minecraft.world.entity.monster.piglin.AbstractPiglin; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.block.Blocks; @@ -48,16 +51,13 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.UUID; +import java.util.*; import java.util.function.Predicate; public class NetherRaid extends BaseRaid { public static final String IDENTIFIER = "nether_raid"; private static final ResourceLocation BARS_RESOURCE = SurvivingTheAftermath.asResource("textures/gui/nether_raid_bars.png"); - protected List spawnPos = Lists.newArrayList(); + protected Set spawnPos = Sets.newHashSet(); private int readyTime; private static final int MAX_REWARD_TIME = 10 * 20; private int rewardTime = MAX_REWARD_TIME; @@ -133,7 +133,7 @@ public int[] getBarsOffset() { @Override public void spawnRewards() { - BlockPos blockPos = spawnPos.get(level.random.nextInt(spawnPos.size())); + BlockPos blockPos = RandomUtils.getRandomElement(spawnPos); Direction dir = Direction.Plane.HORIZONTAL.stream().filter(d -> level.isEmptyBlock(blockPos.relative(d)) && !spawnPos.contains(blockPos.relative(d))).findFirst().orElse(Direction.UP); Vec3 vec = Vec3.atCenterOf(blockPos); @@ -247,10 +247,12 @@ protected List> spawnEntities(IEntityInfoModule module) { for (LazyOptional lazyOptional : arrayList) { lazyOptional.ifPresent(entity -> { if (entity instanceof Mob mob){ - BlockPos blockPos = spawnPos.get(level.random.nextInt(spawnPos.size())); - mob.moveTo(blockPos.getX() + 1, blockPos.getY(), blockPos.getZ() + 1); + BlockPos blockPos = RandomUtils.getRandomElement(spawnPos); + mob.moveTo(blockPos.getX() + 0.5, blockPos.getY() , blockPos.getZ()+ 0.5); mob.setPersistenceRequired(); - mob.getBrain().setMemory(MemoryModuleType.ANGRY_AT, randomPlayersUnderAttack().getUUID()); + Player target = randomPlayersUnderAttack(); + mob.getBrain().setMemory(MemoryModuleType.ANGRY_AT, target.getUUID()); + mob.setTarget(target); for (var slot : EquipmentSlot.values()) { mob.setDropChance(slot, 0); @@ -269,6 +271,8 @@ protected List> spawnEntities(IEntityInfoModule module) { if (entity instanceof Ghast ghast) { ghast.setPos(ghast.getX(), ghast.getY() + 20, ghast.getZ()); } + + enemies.add(mob.getUUID()); level.addFreshEntity(entity); } @@ -277,6 +281,8 @@ protected List> spawnEntities(IEntityInfoModule module) { return arrayList; } + + protected void spawnWave() { if (enemies.isEmpty() && state == AftermathState.ONGOING){ LOGGER.info(getUniqueIdentifier() +": spawn wave " + currentWave); @@ -312,16 +318,14 @@ protected void checkNextWave(){ protected void setSpawnPos(PortalShape portalShape){ spawnPos.clear(); - try { - BlockPos bottomLeft = SurvivingTheAftermath.getPrivateField(portalShape, "bottomLeft", BlockPos.class); - int height = SurvivingTheAftermath.getPrivateField(portalShape, "height", Integer.class); - int width = SurvivingTheAftermath.getPrivateField(portalShape, "width", Integer.class); - Direction rightDir = SurvivingTheAftermath.getPrivateField(portalShape, "rightDir", Direction.class); - BlockPos.betweenClosed(bottomLeft, bottomLeft.relative(Direction.UP, height - 1).relative(rightDir, width - 1)) + spawnPos.add(centerPos); + PortalShapeAccessor portalShapeMixin = (PortalShapeAccessor ) portalShape; + BlockPos bottomLeft = portalShapeMixin.survivingTheAftermath$getBottomLeft(); + int height = portalShapeMixin.survivingTheAftermath$getHeight(); + int width = portalShapeMixin.survivingTheAftermath$getWidth(); + Direction rightDir = portalShapeMixin.survivingTheAftermath$getRightDir(); + BlockPos.betweenClosed(bottomLeft, bottomLeft.relative(Direction.UP, height - 1).relative(rightDir, width - 1)) .forEach(blockPos -> spawnPos.add(new BlockPos(blockPos.getX(), blockPos.getY(), blockPos.getZ()))); - } catch (IllegalAccessException | NoSuchFieldException e) { - LOGGER.error("NetherRaid setSpawnPos error: " + e); - } } @Override @@ -332,6 +336,11 @@ public void bindTrackers() { .forEach(this::addTracker); } + @Override + public boolean join(Entity entity) { + return entity.getType() == EntityType.MAGMA_CUBE && super.join(entity); + } + public static class Factory implements IAftermathFactory { @Override public IAftermath create(ServerLevel level, CompoundTag compound) { diff --git a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/BaseRaid.java b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/BaseRaid.java index 440c6a9..2f84fac 100644 --- a/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/BaseRaid.java +++ b/src/main/java/com/pancake/surviving_the_aftermath/common/raid/api/BaseRaid.java @@ -2,6 +2,7 @@ import com.mojang.logging.LogUtils; +import com.pancake.surviving_the_aftermath.api.AftermathState; import com.pancake.surviving_the_aftermath.api.Constant; import com.pancake.surviving_the_aftermath.api.IAftermath; import com.pancake.surviving_the_aftermath.api.base.BaseAftermath; @@ -86,4 +87,9 @@ public Player randomPlayersUnderAttack(){ } + public boolean join(Entity entity) { + return state == AftermathState.ONGOING + && Math.sqrt(entity.blockPosition().distSqr(centerPos)) < getRadius() + && enemies.add(entity.getUUID()); + } } diff --git a/src/main/resources/surviving_the_aftermath.mixins.json b/src/main/resources/surviving_the_aftermath.mixins.json index ab12d64..6b19f28 100644 --- a/src/main/resources/surviving_the_aftermath.mixins.json +++ b/src/main/resources/surviving_the_aftermath.mixins.json @@ -3,9 +3,10 @@ "minVersion": "0.8", "package": "com.pancake.surviving_the_aftermath.common.mixin", "compatibilityLevel": "JAVA_17", - "refmap": "SurvivingTheAftermath.refmap.json", + "refmap": "surviving_the_aftermath.refmap.json", "mixins": [ - + "PortalShapeMixin", + "VillagerMixin" ], "client": [ ],