Jump to content

cyanog3n

Members
  • Posts

    4
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

cyanog3n's Achievements

Tree Puncher

Tree Puncher (2/8)

0

Reputation

  1. I'm working on a mod that adds fluid pipes to the game, which make use of boolean properties in order to generate the correct model based on the surrounding configuration of pipes and other fluid-capable blocks. They work perfectly, but recently I decided I wanted them to have built-in push and pull functions such that I can extract liquid out of any given block. To do this I replaced the boolean properties with a set of enum properties corresponding to each face which determined their connection type. These also work perfectly, but upon loading up the test client, I noticed that the loading times required to get the the menu screen, and particularly to load up an existing save, were far longer than before. To isolate this I reverted the block properties back to the original set of boolean properties I was using and it loaded up normally. This is my block class: public class TestBlockStateBlock extends GenericXPBlock { public TestBlockStateBlock() { super(Properties.of(Material.METAL) .strength(9f) .destroyTime(1.2f) .requiresCorrectToolForDrops() .explosionResistance(8f) .noOcclusion() .lightLevel(new ToIntFunction<BlockState>() { @Override public int applyAsInt(BlockState value) { return 12; } }) .noOcclusion() .emissiveRendering(new StatePredicate() { @Override public boolean test(BlockState state, BlockGetter getter, BlockPos pos) { return true; } }) .isViewBlocking(new StatePredicate() { @Override public boolean test(BlockState p_61036_, BlockGetter p_61037_, BlockPos p_61038_) { return false; } })); BlockState state = this.getStateDefinition().any(); for(Property<ConnectionType> c : connectionTypes().values()){ state = state.setValue(c, ConnectionType.none); } this.registerDefaultState(state); } //-----SETTING UP ENUM PROPERTIES-----// enum ConnectionType implements StringRepresentable { none, connected, interfaced_idle, interfaced_pull, interfaced_push; @Override public String getSerializedName() { return this.name(); } } public HashMap<String, Property<ConnectionType>> connectionTypes(){ HashMap<String, Property<ConnectionType>> map = new HashMap<>(6); map.put("up", EnumProperty.create("connection_type_up", ConnectionType.class)); map.put("down", EnumProperty.create("connection_type_down", ConnectionType.class)); map.put("north", EnumProperty.create("connection_type_north", ConnectionType.class)); map.put("south", EnumProperty.create("connection_type_south", ConnectionType.class)); map.put("east", EnumProperty.create("connection_type_east", ConnectionType.class)); map.put("west", EnumProperty.create("connection_type_west", ConnectionType.class)); return map; } @Override protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) { for(Property<ConnectionType> c : connectionTypes().values()){ pBuilder.add(c); } } //-----PIPE BEHAVIOR-----// @Override public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { return this.getCollisionShape(pState, pLevel, pPos, pContext); } @Override public VoxelShape getVisualShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { return this.getCollisionShape(pState, pLevel, pPos, pContext); } @Override public VoxelShape getBlockSupportShape(BlockState pState, BlockGetter pReader, BlockPos pPos) { return this.getCollisionShape(pState, pReader, pPos, CollisionContext.empty()); } @Override public VoxelShape getInteractionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos) { return this.getCollisionShape(pState, pLevel, pPos, CollisionContext.empty()); } @Override public VoxelShape getOcclusionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos) { return this.getCollisionShape(pState, pLevel, pPos, CollisionContext.empty()); } @Override public VoxelShape getCollisionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { HashMap<String, VoxelShape> shapes = new HashMap<>(6); VoxelShape core = Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D)); shapes.put("up", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,16 / 16D,10 / 16D))); shapes.put("down", Shapes.create(new AABB(6 / 16D,0 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D))); shapes.put("north", Shapes.create(new AABB(6 / 16D,6 / 16D,0 / 16D,10 / 16D,10 / 16D,10 / 16D))); //-z shapes.put("south", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,16 / 16D))); //+z shapes.put("east", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,16 / 16D,10 / 16D,10 / 16D))); //+x shapes.put("west", Shapes.create(new AABB(0 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D))); //-x VoxelShape finishedshape = core; for(String direction : connectionTypes().keySet()){ if(!pState.getValue(connectionTypes().get(direction)).equals(ConnectionType.none)){ finishedshape = Shapes.join(finishedshape, shapes.get(direction), BooleanOp.OR); } } return finishedshape; } public static HashMap<String, BlockPos> getNeighbors(BlockPos pos){ HashMap<String, BlockPos> neighbors = new HashMap<>(6); neighbors.put("up", pos.above()); neighbors.put("down", pos.below()); neighbors.put("north", pos.north()); neighbors.put("south", pos.south()); neighbors.put("east", pos.east()); neighbors.put("west", pos.west()); return neighbors; } public BlockState generateCorrectState(Level level, BlockPos pos){ BlockState state = this.defaultBlockState(); HashMap<String, BlockPos> neighbors = getNeighbors(pos); for(String direction : neighbors.keySet()){ Block block = level.getBlockState(neighbors.get(direction)).getBlock(); if(block instanceof GenericXPBlock){ state = state.setValue(connectionTypes().get(direction), ConnectionType.connected); } else if(block instanceof ExperienceObeliskBlock){ state = state.setValue(connectionTypes().get(direction), ConnectionType.interfaced_idle); } } return state; } @Nullable @Override public BlockState getStateForPlacement(BlockPlaceContext pContext) { return this.generateCorrectState(pContext.getLevel(), pContext.getClickedPos()); } @Override public void neighborChanged(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock, BlockPos pFromPos, boolean pIsMoving) { BlockState newState = generateCorrectState(pLevel, pPos); if(newState != pState){ pLevel.setBlockAndUpdate(pPos, newState); pLevel.onBlockStateChange(pPos, pState, newState); } } //-----BLOCK ENTITY-----// } And this is the original block class: public class ExperienceDuctBlock extends GenericXPBlock implements EntityBlock { public ExperienceDuctBlock() { super(Properties.of(Material.METAL) .strength(9f) .destroyTime(1.2f) .requiresCorrectToolForDrops() .explosionResistance(8f) .noOcclusion() .lightLevel(new ToIntFunction<BlockState>() { @Override public int applyAsInt(BlockState value) { return 12; } }) .noOcclusion() .emissiveRendering(new StatePredicate() { @Override public boolean test(BlockState state, BlockGetter getter, BlockPos pos) { return true; } }) .isViewBlocking(new StatePredicate() { @Override public boolean test(BlockState p_61036_, BlockGetter p_61037_, BlockPos p_61038_) { return false; } })); BlockState state = this.getStateDefinition().any(); for(Property<Boolean> c : connections().values()){ state = state.setValue(c, false); } for(Property<Boolean> i : interfaces().values()){ state = state.setValue(i, false); } this.registerDefaultState(state); } @Override public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) { boolean isInterfaced = false; for(Property<Boolean> interfaced : interfaces().values()){ if(pState.getValue(interfaced)){ isInterfaced = true; } } if(pLevel.isClientSide() && isInterfaced){ GuiWrapper.openDuctGUI(pState, pLevel, pPos, pPlayer); return InteractionResult.CONSUME; } else{ return InteractionResult.PASS; } } //-----PIPE BEHAVIOR-----// public HashMap<String, Property<Boolean>> connections(){ HashMap<String, Property<Boolean>> map = new HashMap<>(6); map.put("up", BooleanProperty.create("up")); map.put("down", BooleanProperty.create("down")); map.put("north", BooleanProperty.create("north")); map.put("south", BooleanProperty.create("south")); map.put("east", BooleanProperty.create("east")); map.put("west", BooleanProperty.create("west")); return map; } public HashMap<String, Property<Boolean>> interfaces(){ HashMap<String, Property<Boolean>> map = new HashMap<>(6); map.put("up", BooleanProperty.create("interfaced_up")); map.put("down", BooleanProperty.create("interfaced_down")); map.put("north", BooleanProperty.create("interfaced_north")); map.put("south", BooleanProperty.create("interfaced_south")); map.put("east", BooleanProperty.create("interfaced_east")); map.put("west", BooleanProperty.create("interfaced_west")); return map; } public static HashMap<String, BlockPos> getNeighbors(BlockPos pos){ HashMap<String, BlockPos> neighbors = new HashMap<>(6); neighbors.put("up", pos.above()); neighbors.put("down", pos.below()); neighbors.put("north", pos.north()); neighbors.put("south", pos.south()); neighbors.put("east", pos.east()); neighbors.put("west", pos.west()); return neighbors; } @Override protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) { for(Property<Boolean> c : connections().values()){ pBuilder.add(c); } for(Property<Boolean> i : interfaces().values()){ pBuilder.add(i); } } @Override public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { return this.getCollisionShape(pState, pLevel, pPos, pContext); } @Override public VoxelShape getVisualShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { return this.getCollisionShape(pState, pLevel, pPos, pContext); } @Override public VoxelShape getBlockSupportShape(BlockState pState, BlockGetter pReader, BlockPos pPos) { return this.getCollisionShape(pState, pReader, pPos, CollisionContext.empty()); } @Override public VoxelShape getInteractionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos) { return this.getCollisionShape(pState, pLevel, pPos, CollisionContext.empty()); } @Override public VoxelShape getOcclusionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos) { return this.getCollisionShape(pState, pLevel, pPos, CollisionContext.empty()); } @Override public VoxelShape getCollisionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { HashMap<String, VoxelShape> shapes = new HashMap<>(6); VoxelShape core = Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D)); shapes.put("up", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,16 / 16D,10 / 16D))); shapes.put("down", Shapes.create(new AABB(6 / 16D,0 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D))); shapes.put("north", Shapes.create(new AABB(6 / 16D,6 / 16D,0 / 16D,10 / 16D,10 / 16D,10 / 16D))); //-z shapes.put("south", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,16 / 16D))); //+z shapes.put("east", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,16 / 16D,10 / 16D,10 / 16D))); //+x shapes.put("west", Shapes.create(new AABB(0 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D))); //-x VoxelShape finishedshape = core; for(String key : connections().keySet()){ if(pState.getValue(connections().get(key))){ finishedshape = Shapes.join(finishedshape, shapes.get(key), BooleanOp.OR); } if(pState.getValue(interfaces().get(key))){ finishedshape = Shapes.join(finishedshape, shapes.get(key), BooleanOp.OR); } } return finishedshape; } public BlockState generateCorrectState(Level level, BlockPos pos){ BlockState state = this.defaultBlockState(); HashMap<String, BlockPos> neighbors = getNeighbors(pos); for(String key : neighbors.keySet()){ Block block = level.getBlockState(neighbors.get(key)).getBlock(); state = state.setValue(connections().get(key), block instanceof GenericXPBlock); state = state.setValue(interfaces().get(key), block instanceof ExperienceObeliskBlock); } return state; } @Nullable @Override public BlockState getStateForPlacement(BlockPlaceContext pContext) { return this.generateCorrectState(pContext.getLevel(), pContext.getClickedPos()); } @Override public void neighborChanged(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock, BlockPos pFromPos, boolean pIsMoving) { BlockState newState = generateCorrectState(pLevel, pPos); if(newState != pState){ pLevel.setBlockAndUpdate(pPos, newState); pLevel.onBlockStateChange(pPos, pState, newState); } } //-----BLOCK ENTITY-----// @Nullable @Override public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) { return ModTileEntitiesInit.XPDUCT_BE.get().create(pPos, pState); } @Nullable @Override public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) { return pBlockEntityType == ModTileEntitiesInit.XPDUCT_BE.get() ? ExperienceDuctEntity::tick : null; } } Is there some part of my implementation that is causing increased memory usage or some other related problem?
  2. Certainly checks out. I moved everything serverside and it worked like a charm. Thanks for the help!
  3. So If I'm understanding you correctly, the packet should only contain 1. the number of levels i want to store or withdraw and 2. the position of the block i'm targeting? It seems to happen most easily when facing something that causes a large frame drop on the client, like a huge stack of enchantment tables for example. It doesn't happen as easily on low tps situations but I haven't tested this rigorously. During normal gameplay you would have to click the button in excess of ~15cps to get the glitch to happen so I suppose a delay would work well
  4. I have a block that allows you to store XP in the form of a fluid and retrieve it when needed via a GUI. When the buttons are clicked, I send a packet to the server updating the player's XP level as well as the fluid level in the block. It works perfectly most of the time, but I've noticed an case in which the XP is double-counted when the buttons are clicked very rapidly or under very laggy conditions. For instance, supposing you have 10000 mB stored, clicking the '-10' button too fast to retrieve your xp would you getting more than you started with: with 1mB corresponding to 1 experience point. This only seems to happen with the retrieve 1 and 10 buttons while the store buttons work fine. I suspect this is due to the client sending overlapping packets before the server can update the fluid level in the block, allowing you to retrieve more xp than actually exists. However, I have no idea how to prevent this. I've tried making it such that at most one request could sent per tick, as well as updating the client to see if that would help anything but nothing seems to have solved the issue. package com.cyanogen.experienceobelisk.gui; import com.cyanogen.experienceobelisk.block_entities.XPObeliskEntity; import com.cyanogen.experienceobelisk.network.PacketHandler; import com.cyanogen.experienceobelisk.network.UpdateXPToServer; import net.minecraft.world.entity.player.Player; public class XPManager { public static int levelsToXP(int levels){ if (levels <= 16) { return (int) (Math.pow(levels, 2) + 6 * levels); } else if (levels >= 17 && levels <= 31) { return (int) (2.5 * Math.pow(levels, 2) - 40.5 * levels + 360); } else if (levels >= 32) { return (int) (4.5 * Math.pow(levels, 2) - 162.5 * levels + 2220); } return 0; } public static int xpToLevels(int xp){ if (xp < 394) { return (int) (Math.sqrt(xp + 9) - 3); } else if (xp >= 394 && xp < 1628) { return (int) ((Math.sqrt(40 * xp - 7839) + 81) * 0.1); } else if (xp >= 1628) { return (int) ((Math.sqrt(72 * xp - 54215) + 325) / 18); } return 0; } public static int playerXP; public static int finalXP; public static int tickCount = 0; public static void storeXP(int levels, Player player, XPObeliskEntity xpobelisk){ if(player.tickCount != tickCount){ //total amount of experience points the player currently has playerXP = levelsToXP(player.experienceLevel) + Math.round(player.experienceProgress * player.getXpNeededForNextLevel()); if(player.experienceLevel >= levels){ finalXP = levelsToXP(player.experienceLevel - levels) + Math.round(player.experienceProgress * (levelsToXP(player.experienceLevel - levels + 1) - levelsToXP(player.experienceLevel - levels))); player.giveExperienceLevels(-levels); PacketHandler.INSTANCE.sendToServer(new UpdateXPToServer(0,-levels)); xpobelisk.fillFromClient(playerXP - finalXP); } else if (playerXP >= 1){ xpobelisk.fillFromClient(playerXP); PacketHandler.INSTANCE.sendToServer(new UpdateXPToServer(-2147483647,-2147483647)); } } tickCount = player.tickCount; } public static void retrieveXP(int levels, Player player, XPObeliskEntity xpobelisk){ if(player.tickCount != tickCount){ playerXP = levelsToXP(player.experienceLevel) + Math.round(player.experienceProgress * player.getXpNeededForNextLevel()); finalXP = levelsToXP(player.experienceLevel + levels) + Math.round(player.experienceProgress * (levelsToXP(player.experienceLevel + levels + 1) - levelsToXP(player.experienceLevel + levels))); if(xpobelisk.getFluidAmount() >= finalXP - playerXP){ xpobelisk.drain(finalXP - playerXP); xpobelisk.drainFromClient(finalXP - playerXP); PacketHandler.INSTANCE.sendToServer(new UpdateXPToServer(0,levels)); } else if(xpobelisk.getFluidAmount() >= 1){ PacketHandler.INSTANCE.sendToServer(new UpdateXPToServer(xpobelisk.getFluidAmount(),0)); xpobelisk.setFluid(0); xpobelisk.emptyFromClient(); } } tickCount = player.tickCount; } } https://github.com/cyanog3n/experienceobelisk-1.18/blob/master/src/main/java/com/cyanogen/experienceobelisk/gui/XPManager.java Any help would be appreciated!
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.