From fa638c7f47763a87f09bd3b7310a36ed42fff8f5 Mon Sep 17 00:00:00 2001 From: marston Date: Thu, 7 Sep 2023 16:45:59 -0400 Subject: [PATCH] added ore finder and spawner finder --- .../dev/marston/randomloot/loot/LootCase.java | 14 +- .../marston/randomloot/loot/LootUtils.java | 5 +- .../loot/modifiers/ModifierRegistry.java | 6 +- .../loot/modifiers/holders/OreFinder.java | 227 ++++++++++++++++++ .../modifiers/holders/TreasureFinder.java | 227 ++++++++++++++++++ 5 files changed, 475 insertions(+), 4 deletions(-) create mode 100644 src/main/java/dev/marston/randomloot/loot/modifiers/holders/OreFinder.java create mode 100644 src/main/java/dev/marston/randomloot/loot/modifiers/holders/TreasureFinder.java diff --git a/src/main/java/dev/marston/randomloot/loot/LootCase.java b/src/main/java/dev/marston/randomloot/loot/LootCase.java index e216718..ed827cc 100644 --- a/src/main/java/dev/marston/randomloot/loot/LootCase.java +++ b/src/main/java/dev/marston/randomloot/loot/LootCase.java @@ -12,6 +12,9 @@ import net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentContents; import net.minecraft.network.chat.MutableComponent; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.stats.StatType; +import net.minecraft.stats.Stats; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; @@ -39,7 +42,14 @@ public boolean isFoil(ItemStack stack) { public InteractionResultHolder use(Level level, Player player, InteractionHand hand) { ItemStack lootCase = player.getItemInHand(hand); lootCase.shrink(1); // removing case from inventory - + + if (player instanceof ServerPlayer) { + ServerPlayer sPlayer = (ServerPlayer) player; + StatType itemUsed = Stats.ITEM_USED; + + sPlayer.getStats().increment(sPlayer, itemUsed.get(LootRegistry.CaseItem), 1); + } + Modifier.TrackEntityParticle(level, player, ParticleTypes.CLOUD); Thread thread = new Thread() { @@ -50,7 +60,7 @@ public void run() { e.printStackTrace(); } LootUtils.generateTool(player, level); // generate tool and give it to the player - + } }; diff --git a/src/main/java/dev/marston/randomloot/loot/LootUtils.java b/src/main/java/dev/marston/randomloot/loot/LootUtils.java index 8142310..219f2fb 100644 --- a/src/main/java/dev/marston/randomloot/loot/LootUtils.java +++ b/src/main/java/dev/marston/randomloot/loot/LootUtils.java @@ -8,6 +8,7 @@ import javax.json.Json; import javax.json.JsonObject; +import dev.marston.randomloot.RandomLootMod; import dev.marston.randomloot.loot.LootItem.ToolType; import dev.marston.randomloot.loot.modifiers.Modifier; import dev.marston.randomloot.loot.modifiers.ModifierRegistry; @@ -339,7 +340,7 @@ public static void generateTool(Player player, Level level) { if (player instanceof ServerPlayer) { ServerPlayer sPlayer = (ServerPlayer) player; StatType itemUsed = Stats.ITEM_USED; - + count = sPlayer.getStats().getValue(itemUsed.get(LootRegistry.CaseItem)); } @@ -347,6 +348,8 @@ public static void generateTool(Player player, Level level) { int traits = (int) (Math.floor(goodness / 2.0f)); // how many traits the tool should be created with + RandomLootMod.LOGGER.info("generating tool with a goodness score of " + goodness + " and a trait count of " + traits); + LootUtils.setStats(lootItem, goodness); int toolType = (int) (Math.random() * 4); diff --git a/src/main/java/dev/marston/randomloot/loot/modifiers/ModifierRegistry.java b/src/main/java/dev/marston/randomloot/loot/modifiers/ModifierRegistry.java index 0060557..db71f21 100644 --- a/src/main/java/dev/marston/randomloot/loot/modifiers/ModifierRegistry.java +++ b/src/main/java/dev/marston/randomloot/loot/modifiers/ModifierRegistry.java @@ -9,7 +9,9 @@ import dev.marston.randomloot.loot.modifiers.breakers.Learning; import dev.marston.randomloot.loot.modifiers.holders.Effect; import dev.marston.randomloot.loot.modifiers.holders.Hasty; +import dev.marston.randomloot.loot.modifiers.holders.OreFinder; import dev.marston.randomloot.loot.modifiers.holders.Rainy; +import dev.marston.randomloot.loot.modifiers.holders.TreasureFinder; import dev.marston.randomloot.loot.modifiers.hurter.Charging; import dev.marston.randomloot.loot.modifiers.hurter.Combo; import dev.marston.randomloot.loot.modifiers.hurter.Critical; @@ -44,11 +46,13 @@ public class ModifierRegistry { public static Modifier RESISTANT = register(new Effect("Resistant", "resistance", 1, MobEffects.DAMAGE_RESISTANCE)); public static Modifier FIRE_RESISTANT = register(new Effect("Heat Resistant", "fire_resistance", 1, MobEffects.FIRE_RESISTANCE)); public static Modifier RAINY = register(new Rainy()); + public static Modifier ORE_FINDER = register(new OreFinder()); + public static Modifier SPAWNER_FINDER = register(new TreasureFinder()); public static final Set BREAKERS = Set.of(EXPLODE, LEARNING, ATTRACTING); public static final Set USERS = Set.of(TORCH_PLACE, DIRT_PLACE); public static final Set HURTERS = Set.of(CRITICAL, CHARGING, FLAMING, COMBO); - public static final Set HOLDERS = Set.of(HASTY, ABSORBTION, FILLING, RAINY); + public static final Set HOLDERS = Set.of(HASTY, ABSORBTION, FILLING, RAINY, ORE_FINDER, SPAWNER_FINDER); public static Modifier register(Modifier modifier) { diff --git a/src/main/java/dev/marston/randomloot/loot/modifiers/holders/OreFinder.java b/src/main/java/dev/marston/randomloot/loot/modifiers/holders/OreFinder.java new file mode 100644 index 0000000..8490ff1 --- /dev/null +++ b/src/main/java/dev/marston/randomloot/loot/modifiers/holders/OreFinder.java @@ -0,0 +1,227 @@ +package dev.marston.randomloot.loot.modifiers.holders; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Nullable; + +import dev.marston.randomloot.RandomLootMod; +import dev.marston.randomloot.loot.LootUtils; +import dev.marston.randomloot.loot.LootItem.ToolType; +import dev.marston.randomloot.loot.modifiers.BlockBreakModifier; +import dev.marston.randomloot.loot.modifiers.HoldModifier; +import dev.marston.randomloot.loot.modifiers.Modifier; +import dev.marston.randomloot.loot.modifiers.ModifierRegistry; +import dev.marston.randomloot.loot.modifiers.users.TorchPlace; +import net.minecraft.ChatFormatting; +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.effect.MobEffects; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.item.PrimedTnt; +import net.minecraft.world.entity.monster.Shulker; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.Level.ExplosionInteraction; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.phys.AABB; +import net.minecraftforge.event.TickEvent.ServerTickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod.EventBusSubscriber(modid = RandomLootMod.MODID) +public class OreFinder implements HoldModifier{ + + private String name; + private float power; + private final static String POWER = "power"; + + + static int maxTime = 10; + static int time = 0; + static int maxShulkerLife = 10; + + static boolean locked = false; + + private static ArrayList shulkers = new ArrayList(); + private static ArrayList timings = new ArrayList(); + + public OreFinder(String name, float power) { + this.name = name; + this.power = power; + } + + public OreFinder() { + this.name = "Detecting"; + this.power = 4.0f; + } + + public Modifier clone() { + return new OreFinder(); + } + + + + @Override + public CompoundTag toNBT() { + + CompoundTag tag = new CompoundTag(); + + tag.putFloat(POWER, power); + + tag.putString(NAME, name); + + return tag; + } + + @Override + public Modifier fromNBT(CompoundTag tag) { + return new OreFinder(tag.getString(NAME), tag.getFloat(POWER)); + } + + @Override + public String name() { + return name; + } + + @Override + public String tagName() { + return "detecting"; + } + + @Override + public String color() { + return ChatFormatting.WHITE.getName(); + } + + @Override + public String description() { + return "While holding the tool, ores around you will glow."; + } + + @Override + public void writeToLore(List list, boolean shift) { + + MutableComponent comp = Modifier.makeComp(this.name(), this.color()); + + list.add(comp); + } + + + @Override + public Component writeDetailsToLore(@Nullable Level level) { + + return null; + } + + @Override + public boolean compatible(Modifier mod) { + return true; + } + + @Override + public boolean forTool(ToolType type) { + return type.equals(ToolType.PICKAXE) || type.equals(ToolType.AXE) || type.equals(ToolType.SHOVEL); + } + + + + + @SubscribeEvent + public static void tickEvent(ServerTickEvent event) { + locked = true; + + time ++; + time = time % maxTime; + + + if(time == 0) { + int off = 0; + for(int i = 0; i < shulkers.size(); i ++) { + int iOff = i - off; + int tick = timings.get(iOff) + 1; + timings.set(iOff, tick); + Shulker sh = shulkers.get(iOff); + + + + if(tick > maxShulkerLife || sh.level().getBlockState(sh.blockPosition()).getBlock().equals(Blocks.AIR)) { + shulkers.get(iOff).setPos(0, -64, 0); + shulkers.get(iOff).setHealth(0); + shulkers.remove(iOff); + timings.remove(iOff); + off ++; + } + } + } + + locked = false; + } + + @Override + public void hold(ItemStack stack, Level level, Entity holder) { + if (locked) { + return; + } + + int size = 10; + for(int i = -size; i < size; i ++) { + for(int j = -size; j < size; j ++) { + for(int k = -size; k < size; k ++) { + BlockPos p = new BlockPos((int) (holder.getX() + i), (int) (holder.getY() + j) , (int) (holder.getZ() + k)); + Block b = level.getBlockState(p).getBlock(); + name = b.getName().getString(); + + if(name.toLowerCase().contains("ore")) { + + + List entitiesInBlock = level.getEntities(null, new AABB(p)); + if(!entitiesInBlock.isEmpty()) { + boolean isShulker = false; + for (Entity entity : entitiesInBlock) { + if (entity.getType() == EntityType.SHULKER) { + isShulker = true; + break; + } + } + if (isShulker) { + continue; + } + } + + + Shulker se = new Shulker(EntityType.SHULKER, level); + se.setGlowingTag(true); + se.setInvulnerable(true); + se.setInvisible(true); + se.setPos(p.getX(), p.getY(), p.getZ()); + se.setNoAi(true); + + + + level.addFreshEntity(se); + se.addEffect(new MobEffectInstance(MobEffects.INVISIBILITY, 1200, 0, false ,false)); + + + + shulkers.add(se); + timings.add(-1); + + + } + } + } + } + + + + } +} diff --git a/src/main/java/dev/marston/randomloot/loot/modifiers/holders/TreasureFinder.java b/src/main/java/dev/marston/randomloot/loot/modifiers/holders/TreasureFinder.java new file mode 100644 index 0000000..926b6f4 --- /dev/null +++ b/src/main/java/dev/marston/randomloot/loot/modifiers/holders/TreasureFinder.java @@ -0,0 +1,227 @@ +package dev.marston.randomloot.loot.modifiers.holders; + +import java.util.ArrayList; +import java.util.List; + +import javax.annotation.Nullable; + +import dev.marston.randomloot.RandomLootMod; +import dev.marston.randomloot.loot.LootUtils; +import dev.marston.randomloot.loot.LootItem.ToolType; +import dev.marston.randomloot.loot.modifiers.BlockBreakModifier; +import dev.marston.randomloot.loot.modifiers.HoldModifier; +import dev.marston.randomloot.loot.modifiers.Modifier; +import dev.marston.randomloot.loot.modifiers.ModifierRegistry; +import dev.marston.randomloot.loot.modifiers.users.TorchPlace; +import net.minecraft.ChatFormatting; +import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.effect.MobEffects; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.item.PrimedTnt; +import net.minecraft.world.entity.monster.Shulker; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.Level.ExplosionInteraction; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.phys.AABB; +import net.minecraftforge.event.TickEvent.ServerTickEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod.EventBusSubscriber(modid = RandomLootMod.MODID) +public class TreasureFinder implements HoldModifier{ + + private String name; + private float power; + private final static String POWER = "power"; + + + static int maxTime = 10; + static int time = 0; + static int maxShulkerLife = 10; + + static boolean locked = false; + + private static ArrayList shulkers = new ArrayList(); + private static ArrayList timings = new ArrayList(); + + public TreasureFinder(String name, float power) { + this.name = name; + this.power = power; + } + + public TreasureFinder() { + this.name = "Tomb Raider"; + this.power = 4.0f; + } + + public Modifier clone() { + return new TreasureFinder(); + } + + + + @Override + public CompoundTag toNBT() { + + CompoundTag tag = new CompoundTag(); + + tag.putFloat(POWER, power); + + tag.putString(NAME, name); + + return tag; + } + + @Override + public Modifier fromNBT(CompoundTag tag) { + return new TreasureFinder(tag.getString(NAME), tag.getFloat(POWER)); + } + + @Override + public String name() { + return name; + } + + @Override + public String tagName() { + return "spawner"; + } + + @Override + public String color() { + return ChatFormatting.DARK_AQUA.getName(); + } + + @Override + public String description() { + return "While holding the spawners around you will glow."; + } + + @Override + public void writeToLore(List list, boolean shift) { + + MutableComponent comp = Modifier.makeComp(this.name(), this.color()); + + list.add(comp); + } + + + @Override + public Component writeDetailsToLore(@Nullable Level level) { + + return null; + } + + @Override + public boolean compatible(Modifier mod) { + return true; + } + + @Override + public boolean forTool(ToolType type) { + return true; + } + + + + + @SubscribeEvent + public static void tickEvent(ServerTickEvent event) { + locked = true; + + time ++; + time = time % maxTime; + + + if(time == 0) { + int off = 0; + for(int i = 0; i < shulkers.size(); i ++) { + int iOff = i - off; + int tick = timings.get(iOff) + 1; + timings.set(iOff, tick); + Shulker sh = shulkers.get(iOff); + + + + if(tick > maxShulkerLife || sh.level().getBlockState(sh.blockPosition()).getBlock().equals(Blocks.AIR)) { + shulkers.get(iOff).setPos(0, -64, 0); + shulkers.get(iOff).setHealth(0); + shulkers.remove(iOff); + timings.remove(iOff); + off ++; + } + } + } + + locked = false; + } + + @Override + public void hold(ItemStack stack, Level level, Entity holder) { + if (locked) { + return; + } + + int size = 10; + for(int i = -size; i < size; i ++) { + for(int j = -size; j < size; j ++) { + for(int k = -size; k < size; k ++) { + BlockPos p = new BlockPos((int) (holder.getX() + i), (int) (holder.getY() + j) , (int) (holder.getZ() + k)); + Block b = level.getBlockState(p).getBlock(); + + if(b == Blocks.SPAWNER) { + + + List entitiesInBlock = level.getEntities(null, new AABB(p)); + if(!entitiesInBlock.isEmpty()) { + boolean isShulker = false; + for (Entity entity : entitiesInBlock) { + if (entity.getType() == EntityType.SHULKER) { + isShulker = true; + break; + } + } + if (isShulker) { + continue; + } + } + + + Shulker se = new Shulker(EntityType.SHULKER, level); + se.setGlowingTag(true); + + se.setInvulnerable(true); + se.setInvisible(true); + se.setPos(p.getX(), p.getY(), p.getZ()); + se.setNoAi(true); + + + + level.addFreshEntity(se); + se.addEffect(new MobEffectInstance(MobEffects.INVISIBILITY, 1200, 0, false ,false)); + + + + shulkers.add(se); + timings.add(-1); + + + } + } + } + } + + + + } +}