Skip to content

Commit

Permalink
More refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sm0keySa1m0n committed Sep 1, 2023
1 parent 5f7f475 commit a843167
Show file tree
Hide file tree
Showing 29 changed files with 238 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void init() {

this.mode = this.addRenderableWidget(
MultiButton
.builder(ButtonTexture.SMALL_BUTTON, this.cart.getMode())
.builder(ButtonTexture.SMALL_BUTTON, this.cart.mode())
.bounds(centreX + 120, centreY + getYSize() - 100, 40, 16)
.stateCallback(this::setMaintenanceMode)
.tooltipFactory(this::createLockTooltip)
Expand All @@ -59,7 +59,7 @@ public void containerTick() {
}

private void setMaintenanceMode(MaintenanceMinecart.Mode mode) {
if (mode != this.cart.getMode()) {
if (mode != this.cart.mode()) {
this.cart.setMode(mode);
this.updateButtons();
NetworkChannel.GAME.sendToServer(
Expand All @@ -68,7 +68,7 @@ private void setMaintenanceMode(MaintenanceMinecart.Mode mode) {
}

private void updateButtons() {
this.mode.setState(cart.getMode());
this.mode.setState(cart.mode());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ protected void renderBg(GuiGraphics guiGraphics, float partialTicks, int mouseX,
super.renderBg(guiGraphics, partialTicks, mouseX, mouseY);
int x = (width - this.getXSize()) / 2;
int y = (height - this.getYSize()) / 2;
if (this.menu.getLocomotive().getBoiler().hasFuel()) {
int scale = this.menu.getLocomotive().getBoiler().getBurnProgressScaled(12);
if (this.menu.getLocomotive().boiler().hasFuel()) {
int scale = this.menu.getLocomotive().boiler().getBurnProgressScaled(12);
guiGraphics.blit(TEXTURE_LOCATION, x + 99, y + 33 - scale, 176, 59 - scale, 14, scale + 2);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private MultiButton(Builder<T> builder) {
@Override
public void onPress() {
if (!this.locked && this.active) {
this.setState(this.state.getNext());
this.setState(this.state.next());
}
}

Expand All @@ -36,8 +36,8 @@ public void setState(T state) {
if (this.state != state && this.stateCallback != null) {
this.state = state;
this.stateCallback.accept(this.state);
this.setTexturePosition(this.state.getTexturePosition());
this.setMessage(this.state.getLabel());
this.setTexturePosition(this.state.texturePosition());
this.setMessage(this.state.label());
this.setTooltip(this.tooltipFactory.createTooltip(this.state).orElse(null));
}
}
Expand Down Expand Up @@ -75,7 +75,7 @@ public Builder<T> tooltipFactory(TooltipFactory<? super T> tooltipFactory) {
@FunctionalInterface
public interface TooltipFactory<T extends ButtonState<?>> {

TooltipFactory<ButtonState<?>> DEFAULT = state -> state.getTooltip().map(Tooltip::create);
TooltipFactory<ButtonState<?>> DEFAULT = state -> state.tooltip().map(Tooltip::create);

Optional<Tooltip> createTooltip(T state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,31 @@

public class RitualBlockRenderer implements BlockEntityRenderer<RitualBlockEntity> {

private final ItemRenderer itemRenderer;

public RitualBlockRenderer(BlockEntityRendererProvider.Context context) {
this.itemRenderer = context.getItemRenderer();
}

@Override
public void render(RitualBlockEntity blockEntity, float partialTick, PoseStack poseStack,
MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
poseStack.pushPose();
float yOffset = blockEntity.preYOffset +
(blockEntity.yOffset - blockEntity.preYOffset) * partialTick;
poseStack.translate(0.5, 0.95 + yOffset, 0.5);

float yaw = blockEntity.preRotationYaw +
(blockEntity.rotationYaw - blockEntity.preRotationYaw) * partialTick;
poseStack.mulPose(Axis.YP.rotation(yaw));

poseStack.scale(0.6F, 0.6F, 0.6F);

var firestone = new ItemStack(blockEntity.getBlockState().getValue(RitualBlock.CRACKED)
? RailcraftItems.CRACKED_FIRESTONE.get()
: RailcraftItems.REFINED_FIRESTONE.get());

int id = (int) blockEntity.getBlockPos().asLong();
itemRenderer.renderStatic(firestone, ItemDisplayContext.NONE, packedLight, packedOverlay,
poseStack, bufferSource, blockEntity.level(), id);
poseStack.popPose();
}
private final ItemRenderer itemRenderer;

public RitualBlockRenderer(BlockEntityRendererProvider.Context context) {
this.itemRenderer = context.getItemRenderer();
}

@Override
public void render(RitualBlockEntity blockEntity, float partialTick, PoseStack poseStack,
MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
poseStack.pushPose();
var yOffset = blockEntity.getYOffset(partialTick);
poseStack.translate(0.5, 0.95 + yOffset, 0.5);

var yaw = blockEntity.getRotationYaw(partialTick);
poseStack.mulPose(Axis.YP.rotation(yaw));

poseStack.scale(0.6F, 0.6F, 0.6F);

var firestone = new ItemStack(blockEntity.getBlockState().getValue(RitualBlock.CRACKED)
? RailcraftItems.CRACKED_FIRESTONE.get()
: RailcraftItems.REFINED_FIRESTONE.get());

int id = (int) blockEntity.getBlockPos().asLong();
this.itemRenderer.renderStatic(firestone, ItemDisplayContext.NONE, packedLight, packedOverlay,
poseStack, bufferSource, blockEntity.level(), id);
poseStack.popPose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void renderContents(MaintenanceMinecart cart, float partialTicks,
ResourceLocation textureLocation;
if (blinking) {
textureLocation = LAMP_ON_TEX;
} else if (cart.getMode() == MaintenanceMinecart.Mode.OFF) {
} else if (cart.mode() == MaintenanceMinecart.Mode.OFF) {
textureLocation = LAMP_DISABLED_TEX;
} else {
textureLocation = LAMP_OFF_TEX;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/mods/railcraft/gui/button/ButtonState.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

public interface ButtonState<T extends ButtonState<T>> {

Component getLabel();
Component label();

TexturePosition getTexturePosition();
TexturePosition texturePosition();

default Optional<Component> getTooltip() {
default Optional<Component> tooltip() {
return Optional.empty();
}

T getNext();
T next();
}
50 changes: 28 additions & 22 deletions src/main/java/mods/railcraft/world/entity/FirestoneItemEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,47 @@ public FirestoneItemEntity(Level level, Vec3 position, ItemStack itemStack) {
@Override
public void tick() {
super.tick();
if (this.level().isClientSide) {
if (this.level().isClientSide()) {
return;
}
if (++clock % 4 == 0) {
if (++this.clock % 4 == 0) {
FirestoneItem.trySpawnFire(this.level(), this.blockPosition(), getItem(), this.getOwner());
}
}

@Override
public void lavaHurt() {
if (!this.refined || !this.isAlive() || this.level().isClientSide())
if (!this.refined || !this.isAlive() || this.level().isClientSide()) {
return;
}
var firestoneBlock = RailcraftBlocks.RITUAL.get().defaultBlockState();
var surface = this.blockPosition();
if (this.level().getFluidState(surface).is(FluidTags.LAVA)
|| this.level().getFluidState(surface.above()).is(FluidTags.LAVA))
for (int i = 0; i < 10; i++) {
surface = surface.above();
if (this.level().getBlockState(surface).isAir()
&& this.level().getFluidState(surface.below()).is(FluidTags.LAVA)) {
boolean cracked = getItem().getItem() instanceof CrackedFirestoneItem;
if (LevelUtil.setBlockState(this.level(), surface,
firestoneBlock.setValue(RitualBlock.CRACKED, cracked), this.getOwner())) {
var blockEntity = this.level().getBlockEntity(surface);
if (blockEntity instanceof RitualBlockEntity fireTile) {
var firestone = getItem();
fireTile.charge = firestone.getMaxDamage() - firestone.getDamageValue();
if (firestone.hasCustomHoverName())
fireTile.setItemName(firestone.getDisplayName());
this.kill();
return;
}
}
if (!this.level().getFluidState(surface).is(FluidTags.LAVA)
&& !this.level().getFluidState(surface.above()).is(FluidTags.LAVA)) {
return;
}

for (int i = 0; i < 10; i++) {
surface = surface.above();
if (!this.level().getBlockState(surface).isAir()
|| !this.level().getFluidState(surface.below()).is(FluidTags.LAVA)) {
continue;
}

var cracked = getItem().getItem() instanceof CrackedFirestoneItem;
if (LevelUtil.setBlockState(this.level(), surface,
firestoneBlock.setValue(RitualBlock.CRACKED, cracked), this.getOwner())) {
var blockEntity = this.level().getBlockEntity(surface);
if (blockEntity instanceof RitualBlockEntity fireEntity) {
var firestone = getItem();
fireEntity.setCharge(firestone.getMaxDamage() - firestone.getDamageValue());
if (firestone.hasCustomHoverName())
fireEntity.setItemName(firestone.getDisplayName());
this.kill();
return;
}
}
}
}

public boolean isRefined() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ protected void defineSynchedData() {

@Override
public float getMaxCartSpeedOnRail() {
return this.getMode().getSpeed();
return this.mode().speed();
}

public Mode getMode() {
public Mode mode() {
return RailcraftDataSerializers.getEnum(this.entityData, MODE, Mode.values());
}

Expand Down Expand Up @@ -116,7 +116,7 @@ protected void applyNaturalSlowdown() {
@Override
protected void addAdditionalSaveData(CompoundTag tag) {
super.addAdditionalSaveData(tag);
tag.putString("mode", this.getMode().getSerializedName());
tag.putString("mode", this.mode().getSerializedName());
}

@Override
Expand Down Expand Up @@ -167,12 +167,12 @@ private Mode(String name, float speed) {
this.speed = speed;
}

public float getSpeed() {
public float speed() {
return this.speed;
}

@Override
public Component getLabel() {
public Component label() {
return Component.translatable(this.getTranslationKey());
}

Expand All @@ -185,12 +185,12 @@ public String getTipsKey() {
}

@Override
public TexturePosition getTexturePosition() {
public TexturePosition texturePosition() {
return ButtonTexture.SMALL_BUTTON;
}

@Override
public Mode getNext() {
public Mode next() {
return EnumUtil.next(this, values());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void moveAlongTrack(BlockPos pos, BlockState state) {
}

private void placeTrack(BlockPos pos) {
if (getMode() == Mode.OFF)
if (mode() == Mode.OFF)
return;
pos = pos.relative(travelDirection);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mods.railcraft.world.entity.vehicle;

import java.util.EnumSet;
import java.util.Set;
import mods.railcraft.api.track.TrackUtil;
import mods.railcraft.util.container.ContainerTools;
import mods.railcraft.world.entity.RailcraftEntityTypes;
Expand All @@ -25,7 +26,7 @@ public class TrackRelayer extends MaintenancePatternMinecart {
private static final int SLOT_REPLACE = 1;
private static final int[] SLOTS = ContainerTools.buildSlotArray(0, 1);

private static final EnumSet<Direction> HORIZONTAL_DIRECTION =
private static final Set<Direction> HORIZONTAL_DIRECTION =
EnumSet.of(Direction.EAST, Direction.WEST, Direction.NORTH, Direction.SOUTH);

public TrackRelayer(EntityType<?> type, Level level) {
Expand All @@ -49,14 +50,14 @@ public Item getDropItem() {
@Override
public void tick() {
super.tick();
if (level().isClientSide) {
if (this.level().isClientSide()) {
return;
}
if (this.getMode() == Mode.OFF) {
if (this.mode() == Mode.OFF) {
return;
}
stockItems(SLOT_REPLACE, SLOT_STOCK);
replace();
this.stockItems(SLOT_REPLACE, SLOT_STOCK);
this.replace();
}

private void replace() {
Expand All @@ -70,8 +71,8 @@ private void replace() {
var block = blockstate.getBlock();

if (TrackUtil.isRail(block)) {
var trackExist = patternContainer.getItem(SLOT_EXISTS);
var trackStock = patternContainer.getItem(SLOT_STOCK);
var trackExist = this.patternContainer.getItem(SLOT_EXISTS);
var trackStock = this.patternContainer.getItem(SLOT_STOCK);

boolean nextToSuspended = false;
for (var direction : HORIZONTAL_DIRECTION) {
Expand Down Expand Up @@ -107,7 +108,7 @@ public int[] getSlotsForFace(Direction side) {

@Override
public boolean canPlaceItem(int slot, ItemStack stack) {
ItemStack trackReplace = patternContainer.getItem(SLOT_REPLACE);
var trackReplace = this.patternContainer.getItem(SLOT_REPLACE);
return ContainerTools.isItemEqual(stack, trackReplace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void addTravelledTrack(BlockPos pos) {
}

private void removeTrack(BlockPos track) {
if (this.getMode() == Mode.OFF) {
if (this.mode() == Mode.OFF) {
return;
}
if (track.distSqr(this.blockPosition()) >= 9) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public Item getDropItem() {
@Override
public void tick() {
super.tick();
if (this.level().isClientSide) {
if (this.level().isClientSide()) {
return;
}
if (this.getMode() == Mode.OFF) {
if (this.mode() == Mode.OFF) {
return;
}
stockItems(SLOT_REPLACE_UNDER, SLOT_STOCK_UNDER);
Expand Down

0 comments on commit a843167

Please sign in to comment.