actually fix Counter

This commit is contained in:
CreepyCre 2021-02-14 16:00:10 +01:00
parent ea945981de
commit 7e6a142190
2 changed files with 10 additions and 15 deletions

View file

@ -79,9 +79,12 @@ public class RiftConfigurationToolItem extends ModItem {
@Override @Override
public TypedActionResult<Boolean> onAttackBlock(World world, PlayerEntity player, Hand hand, BlockPos pos, Direction direction) { public TypedActionResult<Boolean> onAttackBlock(World world, PlayerEntity player, Hand hand, BlockPos pos, Direction direction) {
if (world.isClient) { if (world.isClient) {
if (player.isSneaking() && Counter.get(player.getStackInHand(hand)).count() != -1 || world.getBlockEntity(pos) instanceof RiftBlockEntity) { if (player.isSneaking()) {
if (Counter.get(player.getStackInHand(hand)).count() != 0 || world.getBlockEntity(pos) instanceof RiftBlockEntity) {
return TypedActionResult.success(true); return TypedActionResult.success(true);
} }
return TypedActionResult.fail(false);
}
} else { } else {
ItemStack stack = player.getStackInHand(hand); ItemStack stack = player.getStackInHand(hand);
if (player.isSneaking()) { if (player.isSneaking()) {
@ -93,8 +96,8 @@ public class RiftConfigurationToolItem extends ModItem {
EntityUtils.chat(player, Text.of("Rift stripped of data and set to invalid id: -1")); EntityUtils.chat(player, Text.of("Rift stripped of data and set to invalid id: -1"));
return TypedActionResult.success(false); return TypedActionResult.success(false);
} }
} else if (Counter.get(stack).count() != -1) { } else if (Counter.get(stack).count() != 0) {
Counter.get(stack).set(-1); Counter.get(stack).reset();
sync(stack, player, hand); sync(stack, player, hand);
EntityUtils.chat(player, Text.of("Counter has been reset.")); EntityUtils.chat(player, Text.of("Counter has been reset."));
@ -113,13 +116,6 @@ public class RiftConfigurationToolItem extends ModItem {
} }
} }
@Override
public ItemStack getDefaultStack() {
ItemStack defaultStack = super.getDefaultStack();
Counter.get(defaultStack).set(-1);
return defaultStack;
}
// TODO: put in ServerPacketHandler // TODO: put in ServerPacketHandler
private void sync(ItemStack stack, PlayerEntity player, Hand hand) { private void sync(ItemStack stack, PlayerEntity player, Hand hand) {
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player; ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;

View file

@ -14,16 +14,15 @@ public class Counter implements Component {
} }
public int increment() { public int increment() {
this.counter++; return this.counter++;
return this.counter;
} }
public int count() { public int count() {
return this.counter; return this.counter;
} }
public void set(int value) { public void reset() {
this.counter = value; this.counter = 0;
} }
public static <T> Counter get(T provider) { public static <T> Counter get(T provider) {