Skip to content

Commit

Permalink
- Slight code optimizations, mainly for the Creator.
Browse files Browse the repository at this point in the history
  • Loading branch information
CobaltTheProtogen committed Jul 19, 2024
1 parent 10d2784 commit 5ccd2a1
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 127 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod_name=YouMatter
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=MIT
# The mod version. See https://semver.org/
mod_version=2.3.1
mod_version=2.3.2
logo=youmatter.png
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/realmayus/youmatter/creator/CreatorBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, Block
*/
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide) {
MenuProvider menuProvider = getMenuProvider(state, level, pos);
if (menuProvider != null) {
player.openMenu(menuProvider, buf -> buf.writeBlockPos(pos));
}
if (level.isClientSide) {
return InteractionResult.SUCCESS;
}
MenuProvider menuProvider = getMenuProvider(state, level, pos);
if (menuProvider != null) {
player.openMenu(menuProvider, buf -> buf.writeBlockPos(pos));
}
return InteractionResult.SUCCESS;
}
Expand Down
152 changes: 65 additions & 87 deletions src/main/java/realmayus/youmatter/creator/CreatorBlockEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package realmayus.youmatter.creator;


import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -33,8 +32,6 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;

public class CreatorBlockEntity extends BlockEntity implements MenuProvider {

Expand All @@ -60,7 +57,7 @@ public void setActivated(boolean activated) {
}
}

public Lazy<ItemStackHandler> inventory = Lazy.of(() -> new ItemStackHandler(5) {
public Lazy < ItemStackHandler > inventory = Lazy.of(() -> new ItemStackHandler(5) {
@Override
protected void onContentsChanged(int slot) {
CreatorBlockEntity.this.setChanged();
Expand Down Expand Up @@ -93,7 +90,7 @@ FluidTank getSTank() {
return sTank;
}

private Lazy<IFluidHandler> fluidHandler = Lazy.of(() -> new IFluidHandler() {
private Lazy < IFluidHandler > fluidHandler = Lazy.of(() -> new IFluidHandler() {
@Override
public int getTanks() {
return 2;
Expand Down Expand Up @@ -176,7 +173,7 @@ public void setEnergy(int energy) {
myEnergyStorage.get().setEnergy(energy);
}

private Lazy<MyEnergyStorage> myEnergyStorage = Lazy.of(() -> new MyEnergyStorage(this, 1000000, Integer.MAX_VALUE));
private Lazy < MyEnergyStorage > myEnergyStorage = Lazy.of(() -> new MyEnergyStorage(this, 1000000, Integer.MAX_VALUE));

@Override
public void setRemoved() {
Expand Down Expand Up @@ -207,7 +204,6 @@ public void load(CompoundTag compound) {
}
}


@Override
public void saveAdditional(CompoundTag compound) {
super.saveAdditional(compound);
Expand Down Expand Up @@ -241,133 +237,115 @@ public static void tick(Level level, BlockPos pos, BlockState state, CreatorBloc
}

public void tick(Level level, BlockPos pos, BlockState state) {
if (currentPartTick != 40 && (currentPartTick % 5) != 0) {
currentPartTick++;
return;
}

if (currentPartTick == 40) { // 2 sec
if (myEnergyStorage.get().getEnergyStored() > 0) {
for (Direction direction : Direction.values()) {
if (isActivated()) {
IEnergyStorage energyStorage = level.getCapability(Capabilities.EnergyStorage.BLOCK, getBlockPos().relative(direction), null);
if (energyStorage != null) {
if (getEnergy() >= 0.3f * 1000000 && sTank.getFluidAmount() >= 125) { // if energy more than 30 % of max energy
if (uTank.getFluidAmount() + YMConfig.CONFIG.productionPerTick.get() <= MAX_UMATTER) {
sTank.drain(125, IFluidHandler.FluidAction.EXECUTE);
uTank.fill(new FluidStack(ModContent.UMATTER.get(), YMConfig.CONFIG.productionPerTick.get()), IFluidHandler.FluidAction.EXECUTE);
myEnergyStorage.get().extractEnergy(Math.round(getEnergy() / 3f), false);
}
}
}
}
}
if (myEnergyStorage.get().getEnergyStored() <= 0) {
currentPartTick = 0;
return;
}

for (Direction direction: Direction.values()) {
if (!isActivated()) continue;

IEnergyStorage e = level.getCapability(Capabilities.EnergyStorage.BLOCK, getBlockPos().relative(direction), null);
if (e == null) continue;

if (getEnergy() < 0.3f * 1000000 || sTank.getFluidAmount() < 125) continue; // if energy less than 30 % of max energy

if (uTank.getFluidAmount() + YMConfig.CONFIG.productionPerTick.get() > MAX_UMATTER) continue;

sTank.drain(125, IFluidHandler.FluidAction.EXECUTE);
uTank.fill(new FluidStack(ModContent.UMATTER.get(), YMConfig.CONFIG.productionPerTick.get()), IFluidHandler.FluidAction.EXECUTE);
myEnergyStorage.get().extractEnergy(Math.round(getEnergy() / 3f), false);
}

//Auto-outputting U-Matter
Object[] neighborTE = getNeighborTileEntity(pos);
if(neighborTE != null) {
IFluidHandler neighborHandler = level.getCapability(Capabilities.FluidHandler.BLOCK, ((BlockPos) neighborTE[0]), (Direction) neighborTE[1]);
if (neighborHandler != null) {
if (uTank.getFluidAmount() >= 500) { // set a maximum output of 500 mB (every two seconds)
uTank.drain(neighborHandler.fill(new FluidStack(ModContent.UMATTER.get(), 500), IFluidHandler.FluidAction.EXECUTE), IFluidHandler.FluidAction.EXECUTE);
} else {
uTank.drain(neighborHandler.fill(new FluidStack(ModContent.UMATTER.get(), uTank.getFluidAmount()), IFluidHandler.FluidAction.EXECUTE), IFluidHandler.FluidAction.EXECUTE);
}
if (neighborTE != null) {
IFluidHandler h = level.getCapability(Capabilities.FluidHandler.BLOCK, ((BlockPos) neighborTE[0]), (Direction) neighborTE[1]);
if (h != null) {
int amountToDrain = Math.min(uTank.getFluidAmount(), 500); // set a maximum output of 500 mB (every two seconds)
uTank.drain(h.fill(new FluidStack(ModContent.UMATTER.get(), amountToDrain), IFluidHandler.FluidAction.EXECUTE), IFluidHandler.FluidAction.EXECUTE);
}
}
currentPartTick = 0;
} else if ((currentPartTick % 5) == 0) { // every five ticks
ItemStack item = inventory.get().getStackInSlot(3);
if (!(item.isEmpty()) && GeneralUtils.canAddItemToSlot(inventory.get().getStackInSlot(4), item, false)) {
if (item.getItem() instanceof BucketItem) {
if (getUTank().getFluidAmount() >= 1000) {
getUTank().drain(1000, IFluidHandler.FluidAction.EXECUTE);
inventory.get().setStackInSlot(3, ItemStack.EMPTY);
inventory.get().insertItem(4, new ItemStack(ModContent.UMATTER_BUCKET.get(), 1), false);
}
if (item.getItem() instanceof BucketItem && getUTank().getFluidAmount() >= 1000) {
getUTank().drain(1000, IFluidHandler.FluidAction.EXECUTE);
inventory.get().setStackInSlot(3, ItemStack.EMPTY);
inventory.get().insertItem(4, new ItemStack(ModContent.UMATTER_BUCKET.get(), 1), false);
} else {
IFluidHandlerItem itemHandler = item.getCapability(Capabilities.FluidHandler.ITEM);
if(itemHandler != null) {
if (itemHandler.getFluidInTank(0).getFluid().isSame(ModContent.UMATTER.get()) || itemHandler.getFluidInTank(0).isEmpty()) {
if (itemHandler.getTankCapacity(0) - itemHandler.getFluidInTank(0).getAmount() < getUTank().getFluidAmount()) { //fluid in S-Tank is more than what fits in the item's tank
getUTank().drain(itemHandler.fill(new FluidStack(ModContent.UMATTER.get(), itemHandler.getTankCapacity(0) - itemHandler.getFluidInTank(0).getAmount()), IFluidHandler.FluidAction.EXECUTE), IFluidHandler.FluidAction.EXECUTE);
} else { //S-Tank's fluid fits perfectly in item's tank
getUTank().drain(itemHandler.fill(getUTank().getFluid(), IFluidHandler.FluidAction.EXECUTE), IFluidHandler.FluidAction.EXECUTE);
}
}
IFluidHandlerItem h = item.getCapability(Capabilities.FluidHandler.ITEM);
if (h != null && (h.getFluidInTank(0).getFluid().isSame(ModContent.UMATTER.get()) || h.getFluidInTank(0).isEmpty())) {
int amountToFill = Math.min(h.getTankCapacity(0) - h.getFluidInTank(0).getAmount(), getUTank().getFluidAmount());
getUTank().drain(h.fill(new FluidStack(ModContent.UMATTER.get(), amountToFill), IFluidHandler.FluidAction.EXECUTE), IFluidHandler.FluidAction.EXECUTE);
}
inventory.get().setStackInSlot(3, ItemStack.EMPTY);
inventory.get().insertItem(4, item, false);
}
}
item = inventory.get().getStackInSlot(1);
if (!item.isEmpty()) {
IFluidHandlerItem itemHandler = item.getCapability(Capabilities.FluidHandler.ITEM);
if(itemHandler != null) {
IFluidHandlerItem h = item.getCapability(Capabilities.FluidHandler.ITEM);
if (h != null) {
if (item.getItem() instanceof BucketItem && GeneralUtils.canAddItemToSlot(inventory.get().getStackInSlot(2), new ItemStack(Items.BUCKET, 1), false)) {
if (!itemHandler.getFluidInTank(0).isEmpty() && (itemHandler.getFluidInTank(0).getFluid().is(Tags.Fluids.STABILIZER))) {
if (!h.getFluidInTank(0).isEmpty() && (h.getFluidInTank(0).getFluid().is(Tags.Fluids.STABILIZER))) {
if (MAX_STABILIZER - getSTank().getFluidAmount() >= 1000) {
getSTank().fill(new FluidStack(itemHandler.getFluidInTank(0).getFluid(), 1000), IFluidHandler.FluidAction.EXECUTE);
getSTank().fill(new FluidStack(h.getFluidInTank(0).getFluid(), 1000), IFluidHandler.FluidAction.EXECUTE);
inventory.get().setStackInSlot(1, ItemStack.EMPTY);
inventory.get().insertItem(2, new ItemStack(Items.BUCKET, 1), false);
}
}
} else if (GeneralUtils.canAddItemToSlot(inventory.get().getStackInSlot(2), item, false)) {
if (itemHandler.getFluidInTank(0).getFluid().is(Tags.Fluids.STABILIZER)) {
if (itemHandler.getFluidInTank(0).getAmount() > MAX_STABILIZER - getSTank().getFluidAmount()) { //given fluid is more than what fits in the S-Tank
getSTank().fill(itemHandler.drain(MAX_STABILIZER - getSTank().getFluidAmount(), IFluidHandler.FluidAction.EXECUTE), IFluidHandler.FluidAction.EXECUTE);
} else { //given fluid fits perfectly in S-Tank
getSTank().fill(itemHandler.drain(itemHandler.getFluidInTank(0).getAmount(), IFluidHandler.FluidAction.EXECUTE), IFluidHandler.FluidAction.EXECUTE);
}
if (h.getFluidInTank(0).getFluid().is(Tags.Fluids.STABILIZER)) {
int amountToDrain = Math.min(h.getFluidInTank(0).getAmount(), MAX_STABILIZER - getSTank().getFluidAmount());
getSTank().fill(h.drain(amountToDrain, IFluidHandler.FluidAction.EXECUTE), IFluidHandler.FluidAction.EXECUTE);
}
}
}
inventory.get().setStackInSlot(1, ItemStack.EMPTY);
inventory.get().insertItem(2, item, false);
}
currentPartTick++;
} else {
currentPartTick++;
}
}

private Object[] getNeighborTileEntity(BlockPos creatorPos) {
HashMap<BlockPos, Direction> foundPos = new HashMap<>();
for (Direction facing : Direction.values()) {
Object[] result = null;

for (Direction facing: Direction.values()) {
BlockPos offsetPos = creatorPos.relative(facing);
BlockEntity offsetBe = level.getBlockEntity(offsetPos);

if (offsetBe != null) {
IFluidHandler fluidHandler = level.getCapability(Capabilities.FluidHandler.BLOCK, offsetPos, facing);
if (fluidHandler != null) {
foundPos.put(offsetPos, facing);
}
}
}

// Prioritize Replicator
for (Map.Entry<BlockPos, Direction> entry : foundPos.entrySet()) {
if (level.getBlockEntity(entry.getKey()) instanceof ReplicatorBlockEntity replicator) {
IFluidHandler h = level.getCapability(Capabilities.FluidHandler.BLOCK, replicator.getBlockPos(), entry.getValue());
if (h != null && h.fill(new FluidStack(ModContent.UMATTER.get(), 500), IFluidHandler.FluidAction.SIMULATE) > 0) {
//Replicator can take fluid
return new Object[]{entry.getKey(), entry.getValue()}; // position, facing
}
}
}

// Replicator not found / can't take fluid, now trying other blocks
for (Map.Entry<BlockPos, Direction> entry : foundPos.entrySet()) {
BlockEntity be = level.getBlockEntity(entry.getKey());
if (be != null) {
IFluidHandler h = level.getCapability(Capabilities.FluidHandler.BLOCK, be.getBlockPos(), entry.getValue());
IFluidHandler h = level.getCapability(Capabilities.FluidHandler.BLOCK, offsetPos, facing);
if (h != null && h.fill(new FluidStack(ModContent.UMATTER.get(), 500), IFluidHandler.FluidAction.SIMULATE) > 0) {
//Tile can take fluid
return new Object[]{entry.getKey(), entry.getValue()}; // position, facing
if (offsetBe instanceof ReplicatorBlockEntity) {
//Replicator can take fluid
return new Object[] {
offsetPos,
facing
}; // position, facing
} else if (result == null) {
//Tile can take fluid
result = new Object[] {
offsetPos,
facing
}; // position, facing
}
}
}
}

// found nothing
return null;
return result; // found nothing or return the first non-replicator that can take fluid
}


@Override
public Component getDisplayName() {
return Component.translatable(ModContent.CREATOR_BLOCK.get().getDescriptionId());
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/realmayus/youmatter/creator/CreatorScreen.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package realmayus.youmatter.creator;

import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
Expand Down Expand Up @@ -49,12 +50,7 @@ protected void renderBg(GuiGraphics guiGraphics, float partialTicks, int mouseX,
}

private void drawActiveIcon(GuiGraphics guiGraphics, boolean isActive) {
if(isActive) {
guiGraphics.blit(GUI, 154, 13, 176, 24, 8, 9);

} else {
guiGraphics.blit(GUI, 154, 13, 176, 15, 8, 9);
}
guiGraphics.blit(GUI, 154, 13, 176, isActive ? 24 : 15, 8, 9);
}


Expand Down Expand Up @@ -90,7 +86,11 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partia
int yAxis = (mouseY - (height - imageHeight) / 2);

if(xAxis >= 31 && xAxis <= 44 && yAxis >= 20 && yAxis <= 75) {
drawTooltip(guiGraphics, mouseX, mouseY, Arrays.asList(Component.literal(I18n.get("youmatter.gui.stabilizer.title")), Component.literal(I18n.get("youmatter.gui.stabilizer.description", creator.getSTank().getFluidAmount()))));
if (!creator.getSTank().isEmpty()) {
drawTooltip(guiGraphics, mouseX, mouseY, Arrays.asList(Component.literal(I18n.get(creator.getSTank().getFluidInTank(0).getTranslationKey())).withStyle(ChatFormatting.GOLD), Component.literal(I18n.get("youmatter.gui.stabilizer.description", creator.getSTank().getFluidAmount()))));
} else {
drawTooltip(guiGraphics, mouseX, mouseY, Arrays.asList(Component.literal(I18n.get("youmatter.gui.stabilizer.title")), Component.literal(I18n.get("youmatter.gui.stabilizer.description", creator.getSTank().getFluidAmount()))));
}
}
if(xAxis >= 89 && xAxis <= 102 && yAxis >= 20 && yAxis <= 75) {
drawTooltip(guiGraphics, mouseX, mouseY, Arrays.asList(Component.literal(I18n.get("youmatter.gui.umatter.title")), Component.literal(I18n.get("youmatter.gui.umatter.description", creator.getUTank().getFluidAmount()))));
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/realmayus/youmatter/encoder/EncoderBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, Block
*/
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide) {
MenuProvider menuProvider = getMenuProvider(state, level, pos);
if (menuProvider != null) {
player.openMenu(menuProvider, buf -> buf.writeBlockPos(pos));
}
}
if (level.isClientSide) {
return InteractionResult.SUCCESS;
}
MenuProvider menuProvider = getMenuProvider(state, level, pos);
if (menuProvider != null) {
player.openMenu(menuProvider, buf -> buf.writeBlockPos(pos));
}
return InteractionResult.SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void tick(Level level, BlockPos pos, BlockState state, EncoderBloc
}

public void tick(Level level, BlockPos pos, BlockState state) {
if (queue.size() > 0) {
if (!queue.isEmpty()) {
ItemStack processIS = queue.get(queue.size() - 1);
if (processIS != ItemStack.EMPTY) {
if (inventory != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, Block
*/
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide) {
MenuProvider menuProvider = getMenuProvider(state, level, pos);
if (menuProvider != null) {
player.openMenu(menuProvider, buf -> buf.writeBlockPos(pos));
}
if (level.isClientSide) {
return InteractionResult.SUCCESS;
}
MenuProvider menuProvider = getMenuProvider(state, level, pos);
if (menuProvider != null) {
player.openMenu(menuProvider, buf -> buf.writeBlockPos(pos));
}
return InteractionResult.SUCCESS;
}
Expand Down
Loading

0 comments on commit 5ccd2a1

Please sign in to comment.