Skip to content

Commit

Permalink
Initial Work on TeleportDrops and Xray Abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Mar 12, 2024
1 parent 03c3e77 commit b0b0577
Show file tree
Hide file tree
Showing 17 changed files with 189 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.20.4 2024-03-11T19:49:00.7736791 Languages: en_us for mod: justdirethings
b43100a43c2470204006e31370ed7becadba6ca6 assets/justdirethings/lang/en_us.json
// 1.20.4 2024-03-11T22:46:10.0136183 Languages: en_us for mod: justdirethings
bb6ed393fbe445017cf36ea6a1bc8f2f4dc62de5 assets/justdirethings/lang/en_us.json
2 changes: 2 additions & 0 deletions src/generated/resources/assets/justdirethings/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
"item.justdirethings.raw_blazegold": "Raw Blazegold",
"item.justdirethings.raw_ferricore": "Raw Ferricore",
"itemGroup.DeferredHolder{ResourceKey[minecraft:creative_mode_tab / justdirethings:justdirethings]}": "Just Dire Things",
"justdirethings.ability.dropteleport": "Drops Teleporter",
"justdirethings.ability.hammer": "Hammer",
"justdirethings.ability.hammervalue": "Hammer: %s",
"justdirethings.ability.lawnmower": "Lawnmower",
"justdirethings.ability.leafbreaker": "Leaf Breaker",
"justdirethings.ability.mobscanner": "Mob Scanner",
"justdirethings.ability.oreminer": "Ore Miner",
"justdirethings.ability.orescanner": "Ore Scanner",
"justdirethings.ability.orexray": "X-Ray",
"justdirethings.ability.skysweeper": "Sky Sweeper",
"justdirethings.ability.smelter": "Auto Smelter",
"justdirethings.ability.treefeller": "Tree Feller",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static void render(RenderLevelStageEvent evt, Player player, ItemStack he
public static void discover(Player player, Ability toolAbility) {
if (toolAbility.equals(Ability.MOBSCANNER))
discoverMobs(player, true);
else if (toolAbility.equals(Ability.ORESCANNER))
else if (toolAbility.equals(Ability.ORESCANNER) || toolAbility.equals(Ability.OREXRAY))
discoverOres(player, toolAbility);
}

Expand All @@ -100,10 +100,10 @@ private static void discoverOres(Player player, Ability toolAbility) {
.filter(blockPos -> player.level().getBlockState(blockPos).getTags().anyMatch(tag -> tag.equals(Tags.Blocks.ORES)))
.map(BlockPos::immutable)
.collect(Collectors.toList());
if (!toolAbility.equals(Ability.ORESCANNER)) { //Todo Proper
if (toolAbility.equals(Ability.OREXRAY)) {
xRayStartTime = System.currentTimeMillis();
generateVBO(player);
} else {
} else if (toolAbility.equals(Ability.ORESCANNER)) {
blockParticlesStartTime = System.currentTimeMillis();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
public class CelestigemPickaxe extends BasePickaxe implements PoweredItem {
public CelestigemPickaxe() {
super(GooTier.CELESTIGEM, 1, -2.8F, new Properties());
registerAbility(Ability.ORESCANNER);
registerAbility(Ability.OREXRAY);
registerAbility(Ability.OREMINER);
registerAbility(Ability.SMELTER);
registerAbility(Ability.HAMMER, new AbilityParams(3, 3, 2));
registerAbility(Ability.HAMMER, new AbilityParams(3, 5, 2));
registerAbility(Ability.DROPTELEPORT);
}

@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
scanFor(level, player, hand, Ability.ORESCANNER);
scanFor(level, player, hand, Ability.OREXRAY);
return super.use(level, player, hand);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

Expand All @@ -32,6 +34,12 @@ public BaseAxe(Tier pTier, float pAttackDamageModifier, float pAttackSpeedModifi
super(pTier, pAttackDamageModifier, pAttackSpeedModifier, pProperties);
}

@Override
public InteractionResult useOn(UseOnContext pContext) {
bindDrops(pContext);
return super.useOn(pContext);
}

@Override
public boolean mineBlock(ItemStack pStack, Level pLevel, BlockState pState, BlockPos pPos, LivingEntity pEntityLiving) {
return true; //We handle damage in the BlockEvent.BreakEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

Expand All @@ -28,6 +30,12 @@ public BaseHoe(Tier pTier, int pAttackDamageModifier, float pAttackSpeedModifier
super(pTier, pAttackDamageModifier, pAttackSpeedModifier, pProperties);
}

@Override
public InteractionResult useOn(UseOnContext pContext) {
bindDrops(pContext);
return super.useOn(pContext);
}

@Override
public boolean mineBlock(ItemStack pStack, Level pLevel, BlockState pState, BlockPos pPos, LivingEntity pEntityLiving) {
return true; //We handle damage in the BlockEvent.BreakEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

Expand All @@ -33,6 +37,12 @@ public BasePickaxe(Tier pTier, int pAttackDamageModifier, float pAttackSpeedModi
super(pTier, pAttackDamageModifier, pAttackSpeedModifier, pProperties);
}

@Override
public InteractionResult useOn(UseOnContext pContext) {
bindDrops(pContext);
return super.useOn(pContext);
}

@Override
public boolean mineBlock(ItemStack pStack, Level pLevel, BlockState pState, BlockPos pPos, LivingEntity pEntityLiving) {
return true; //We handle damage in the BlockEvent.BreakEvent
Expand All @@ -54,6 +64,7 @@ public void appendHoverText(ItemStack stack, @javax.annotation.Nullable Level le
}
boolean sneakPressed = Screen.hasShiftDown();
if (stack.getItem() instanceof ToggleableTool toggleableTool) {
GlobalPos boundInventory = ToggleableTool.getBoundInventory(stack);
PoweredItem.appendFEText(stack, level, tooltip, flagIn);
if (sneakPressed) {
if (ToggleableTool.getEnabled(stack))
Expand All @@ -74,6 +85,15 @@ public void appendHoverText(ItemStack stack, @javax.annotation.Nullable Level le
boolean active = ToggleableTool.getSetting(stack, ability.getName());
ChatFormatting chatFormatting = active ? ChatFormatting.GREEN : ChatFormatting.DARK_RED;
tooltip.add(Component.translatable(ability.getLocalization()).withStyle(chatFormatting));
if (ability.equals(Ability.DROPTELEPORT)) {
chatFormatting = ChatFormatting.DARK_PURPLE;
String dimString;
if (boundInventory == null)
dimString = " -Unbound";
else
dimString = " -" + I18n.get(boundInventory.dimension().location().getPath()) + ": [" + boundInventory.pos().getX() + "," + boundInventory.pos().getY() + "," + boundInventory.pos().getZ() + "]";
tooltip.add(Component.literal(dimString).withStyle(chatFormatting));
}
}
} else {
if (ToggleableTool.getEnabled(stack))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

Expand All @@ -31,6 +33,12 @@ public BaseShovel(Tier pTier, float pAttackDamageModifier, float pAttackSpeedMod
super(pTier, pAttackDamageModifier, pAttackSpeedModifier, pProperties);
}

@Override
public InteractionResult useOn(UseOnContext pContext) {
bindDrops(pContext);
return super.useOn(pContext);
}

@Override
public boolean mineBlock(ItemStack pStack, Level pLevel, BlockState pState, BlockPos pPos, LivingEntity pEntityLiving) {
return true; //We handle damage in the BlockEvent.BreakEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

Expand All @@ -36,6 +38,12 @@ public boolean mineBlock(ItemStack pStack, Level pLevel, BlockState pState, Bloc
return true; //We handle damage in the BlockEvent.BreakEvent
}

@Override
public InteractionResult useOn(UseOnContext pContext) {
bindDrops(pContext);
return super.useOn(pContext);
}

@Override
public boolean hurtEnemy(ItemStack pStack, LivingEntity pTarget, LivingEntity pAttacker) {
return hurtEnemyAbility(pStack, pTarget, pAttacker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
import java.util.Map;

public enum Ability {
//Tier 1
MOBSCANNER(SettingType.TOGGLE, 10, 100), //TODO Configs
OREMINER(SettingType.TOGGLE, 1, 10),
ORESCANNER(SettingType.TOGGLE, 10, 100),
LAWNMOWER(SettingType.TOGGLE, 1, 10),
SKYSWEEPER(SettingType.TOGGLE, 1, 10),
TREEFELLER(SettingType.TOGGLE, 1, 10),
LEAFBREAKER(SettingType.TOGGLE, 1, 10),
//Tier 2
SMELTER(SettingType.TOGGLE, 1, 10),
HAMMER(SettingType.CYCLE, 1, 10);
HAMMER(SettingType.CYCLE, 1, 10),
//Tier 3
OREXRAY(SettingType.TOGGLE, 100, 1000),
DROPTELEPORT(SettingType.TOGGLE, 10, 100);

public enum SettingType {
TOGGLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.event.level.BlockEvent;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.ItemHandlerHelper;

import java.util.*;
import java.util.function.Predicate;
Expand Down Expand Up @@ -189,6 +191,19 @@ public static void dropDrops(List<ItemStack> drops, ServerLevel level, BlockPos
}
}

public static void teleportDrops(List<ItemStack> drops, IItemHandler handler) {
List<ItemStack> leftovers = new ArrayList<>();
for (ItemStack drop : drops) {
ItemStack leftover = ItemHandlerHelper.insertItemStacked(handler, drop, false);
if (!leftover.isEmpty()) {
leftovers.add(leftover);
}
}
// Clear the original drops list and add all leftovers to it
drops.clear();
drops.addAll(leftovers);
}

public static Set<BlockPos> findLikeBlocks(Level pLevel, BlockState pState, BlockPos pPos, Direction direction, int maxBreak, int range) {
if (direction == null)
return findBlocks(pLevel, pState, pPos, maxBreak, range);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import com.direwolf20.justdirethings.common.events.BlockToolModification;
import com.direwolf20.justdirethings.datagen.JustDireBlockTags;
import com.direwolf20.justdirethings.util.MiningCollect;
import com.direwolf20.justdirethings.util.MiscHelpers;
import com.direwolf20.justdirethings.util.NBTUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.GlobalPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
Expand All @@ -23,8 +26,11 @@
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.items.IItemHandler;
import org.jetbrains.annotations.NotNull;

import java.util.*;
Expand Down Expand Up @@ -109,6 +115,16 @@ default void mineBlocksAbility(ItemStack pStack, Level pLevel, BlockPos pPos, Li
if (smeltedItemsFlag[0])
smelterParticles((ServerLevel) pLevel, breakBlockPositions);
}
if (!drops.isEmpty() && canUseAbility(pStack, Ability.DROPTELEPORT)) {
GlobalPos globalPos = getBoundInventory(pStack);
if (globalPos != null) {
IItemHandler handler = MiscHelpers.getAttachedInventory(pLevel.getServer().getLevel(globalPos.dimension()), globalPos.pos(), getBoundInventorySide(pStack));
if (handler != null) {
teleportDrops(drops, handler);
teleportParticles((ServerLevel) pLevel, breakBlockPositions);
}
}
}
if (!drops.isEmpty()) {
Helpers.dropDrops(drops, (ServerLevel) pLevel, pPos);
pState.getBlock().popExperience((ServerLevel) pLevel, pPos, totalExp);
Expand All @@ -127,6 +143,18 @@ default void smelterParticles(ServerLevel level, Set<BlockPos> oreBlocksList) {
}
}

default void teleportParticles(ServerLevel level, Set<BlockPos> oreBlocksList) {
Random random = new Random();
for (int i = 0; i < 5; i++) {
for (BlockPos pos : oreBlocksList) {
double d0 = (double) pos.getX() + random.nextDouble();
double d1 = (double) pos.getY() + random.nextDouble();
double d2 = (double) pos.getZ() + random.nextDouble();
level.sendParticles(ParticleTypes.PORTAL, d0, d1, d2, 1, 0.0, 0.0, 0.0, 0);
}
}
}

default boolean scanFor(Level level, Player player, InteractionHand hand, Ability toolAbility) {
if (!player.isShiftKeyDown()) {
ItemStack itemStack = player.getItemInHand(hand);
Expand Down Expand Up @@ -186,6 +214,23 @@ default boolean lawnmower(Level level, Player player, InteractionHand hand) {
return false;
}

default boolean bindDrops(UseOnContext pContext) {
Player player = pContext.getPlayer();
if (player == null) return false;
if (!player.isShiftKeyDown()) return false;
ItemStack pStack = pContext.getItemInHand();
if (!(pStack.getItem() instanceof ToggleableTool toggleableTool)) return false;
if (!toggleableTool.hasAbility(Ability.DROPTELEPORT)) return false;
Level pLevel = pContext.getLevel();
BlockPos pPos = pContext.getClickedPos();
BlockEntity blockEntity = pLevel.getBlockEntity(pPos);
if (blockEntity == null) return false;
IItemHandler handler = pLevel.getCapability(Capabilities.ItemHandler.BLOCK, pPos, pContext.getClickedFace());
if (handler == null) return false;
setBoundInventory(pStack, GlobalPos.of(pLevel.dimension(), pPos), pContext.getClickedFace());
return true;
}

//Thanks Soaryn!
@NotNull
static Direction getTargetLookDirection(LivingEntity livingEntity) {
Expand Down Expand Up @@ -234,6 +279,26 @@ static boolean cycleSetting(ItemStack stack, String setting) {
return tagCompound.getBoolean(setting);
}

static void setBoundInventory(ItemStack stack, GlobalPos bindLocation, Direction side) {
CompoundTag tagCompound = stack.getOrCreateTag();
tagCompound.put("boundinventory", NBTUtils.globalPosToNBT(bindLocation));
tagCompound.putInt("boundinventory_side", side.ordinal());
}

static GlobalPos getBoundInventory(ItemStack stack) {
CompoundTag tagCompound = stack.getOrCreateTag();
if (tagCompound.contains("boundinventory"))
return NBTUtils.nbtToGlobalPos(tagCompound.getCompound("boundinventory"));
return null;
}

static Direction getBoundInventorySide(ItemStack stack) {
CompoundTag tagCompound = stack.getOrCreateTag();
if (tagCompound.contains("boundinventory_side"))
return Direction.values()[tagCompound.getInt("boundinventory_side")];
return null;
}

static boolean setSetting(ItemStack stack, String setting, boolean value) {
CompoundTag tagCompound = stack.getOrCreateTag();
tagCompound.putBoolean(setting, value);
Expand Down
Loading

0 comments on commit b0b0577

Please sign in to comment.