Skip to content

Commit

Permalink
Fixed Crash when fake players use Deforester & Excavation Drill & fix…
Browse files Browse the repository at this point in the history
… randomizing chance
  • Loading branch information
LopyLuna committed Sep 22, 2024
1 parent 99fc452 commit 88ffe2d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
16 changes: 8 additions & 8 deletions src/main/java/uwu/lopyluna/create_dd/DesireUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ public class DesireUtil {


public static boolean randomChance(int chance) {
int newChance = 100 - Mth.clamp(chance, 1, 100);
return (newChance + 1 == 1) ? true : RANDOM.nextInt(1, newChance + 2) == 1;
int newChance = Mth.clamp(chance, 0, 100);
return RANDOM.nextInt(1, 100) <= newChance;
}

public static boolean randomChance(int chance, Level level) {
int newChance = 100 - Mth.clamp(chance, 1, 100);
return (newChance + 1 == 1) ? true : level.getRandom().nextInt(1, newChance + 1) == 1;
int newChance = Mth.clamp(chance, 0, 100);
return level.getRandom().nextInt(1, 100) <= newChance;
}

public static boolean randomChance(double chance) {
int newChance = 100 - Mth.clamp(((int) chance * 100), 1, 100);
return (newChance + 1 == 1) ? true : RANDOM.nextInt(1, newChance + 2) == 1;
int newChance = Mth.clamp(((int) chance * 100), 0, 100);
return RANDOM.nextInt(1, 100) <= newChance;
}
public static boolean randomChance(double chance, Level level) {
int newChance = 100 - Mth.clamp(((int) chance * 100), 1, 100);
return (newChance + 1 == 1) ? true : level.getRandom().nextInt(1, newChance + 2) == 1;
int newChance = Mth.clamp(((int) chance * 100), 0, 100);
return level.getRandom().nextInt(1, 100) <= newChance;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.level.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand Down Expand Up @@ -50,25 +51,32 @@ public DeforesterSawItem(Properties pProperties) {
super(Deforester, 6.0F, -3.2F, pProperties);
}

@SuppressWarnings("all")
public static void destroyTree(Level pLevel, BlockState state, BlockPos pos, Player player) {
DesiresPackets.getChannel().sendToServer(new InvertFunctionPacket());
boolean flagFakePlayer = player != null && !(player instanceof FakePlayer);
boolean playerHeldShift;

if (flagFakePlayer) {
DesiresPackets.getChannel().sendToServer(new InvertFunctionPacket());
boolean inverted = DesiresConfigs.client().invertDeforesterSawFunction.get();
boolean playerHeldShift = inverted != player.isShiftKeyDown();
playerHeldShift = inverted != player.isShiftKeyDown();
} else {
playerHeldShift = true;
}

if (deforesting || !(state.is(BlockTags.LOGS) || AllTags.AllBlockTags.SLIMY_LOGS.matches(state)) || !playerHeldShift)
return;
Vec3 vec = player.getLookAngle();
if (deforesting || !(state.is(BlockTags.LOGS) || AllTags.AllBlockTags.SLIMY_LOGS.matches(state)) || !playerHeldShift)
return;
Vec3 vec = player.getLookAngle();

deforesting = true;
Optional<AbstractBlockBreakQueue> dynamicTree = findDynamicTree(state.getBlock(), pos);
if (dynamicTree.isPresent()) {
dynamicTree.get().destroyBlocks(pLevel, player, (dropPos, item) -> dropItemFromCutTree(pLevel, pos, vec, dropPos, item));
deforesting = false;
return;
}
findTree(pLevel, pos).destroyBlocks(pLevel, player, (dropPos, item) -> dropItemFromCutTree(pLevel, pos, vec, dropPos, item));
deforesting = true;
Optional<AbstractBlockBreakQueue> dynamicTree = findDynamicTree(state.getBlock(), pos);
if (dynamicTree.isPresent()) {
dynamicTree.get().destroyBlocks(pLevel, player, (dropPos, item) -> dropItemFromCutTree(pLevel, pos, vec, dropPos, item));
deforesting = false;
return;
}
findTree(pLevel, pos).destroyBlocks(pLevel, player, (dropPos, item) -> dropItemFromCutTree(pLevel, pos, vec, dropPos, item));
deforesting = false;
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.level.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand Down Expand Up @@ -66,11 +67,18 @@ public void inventoryTick(ItemStack pStack, Level pLevel, Entity pEntity, int pS
super.inventoryTick(pStack, pLevel, pEntity, pSlotId, pIsSelected);
}

@SuppressWarnings("all")
public static void destroyVein(Level pLevel, BlockState state, BlockPos pos, Player player) {
DesiresPackets.getChannel().sendToServer(new InvertFunctionPacket());
boolean flagFakePlayer = player != null && !(player instanceof FakePlayer);
boolean playerHeldShift;

boolean inverted = DesiresConfigs.client().invertDeforesterSawFunction.get();
boolean playerHeldShift = inverted != player.isShiftKeyDown();
if (flagFakePlayer) {
DesiresPackets.getChannel().sendToServer(new InvertFunctionPacket());
boolean inverted = DesiresConfigs.client().invertExcavationDrillFunction.get();
playerHeldShift = inverted != player.isShiftKeyDown();
} else {
playerHeldShift = true;
}

if (veinExcavating || !(state.is(EXCAVATION_DRILL_VEIN_VALID.tag)) || !playerHeldShift)
return;
Expand All @@ -90,13 +98,20 @@ public static void dropItemFromExcavatedVein(Level world, BlockPos breakingPos,
world.addFreshEntity(entity);
}

@SuppressWarnings("all")
@SubscribeEvent
public static void onBlockDestroyed(BlockEvent.BreakEvent event) {
Player player = event.getPlayer();
DesiresPackets.getChannel().sendToServer(new InvertFunctionPacket());
boolean flagFakePlayer = player != null && !(player instanceof FakePlayer);
boolean playerHeldShift;

boolean inverted = DesiresConfigs.client().invertDeforesterSawFunction.get();
boolean playerHeldShift = inverted != player.isShiftKeyDown();
if (flagFakePlayer) {
DesiresPackets.getChannel().sendToServer(new InvertFunctionPacket());
boolean inverted = DesiresConfigs.client().invertExcavationDrillFunction.get();
playerHeldShift = inverted != player.isShiftKeyDown();
} else {
playerHeldShift = true;
}

Level level = (Level) event.getLevel();
ItemStack heldItemMainhand = event.getPlayer().getItemInHand(InteractionHand.MAIN_HAND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static void regStrippables() {
public static void regStrippables(WoodEntry entry) {
AxeItem.STRIPPABLES = Maps.newHashMap(AxeItem.STRIPPABLES);
AxeItem.STRIPPABLES.put(entry.log.get(), entry.strippedLog.get());
AxeItem.STRIPPABLES.put(entry.wood.get(), entry.strippedWood.get());
}

public static void register() {}
Expand Down

0 comments on commit 88ffe2d

Please sign in to comment.