Skip to content

Commit

Permalink
Merge branch '1.20.x' of https://github.com/Dawnwalker666/Botania int…
Browse files Browse the repository at this point in the history
…o 1.20.x
  • Loading branch information
Dawnwalker666 committed Nov 14, 2023
2 parents 0bf22df + 99c1cc6 commit 4a5fb68
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,16 @@ public TriPredicate<BlockGetter, BlockPos, BlockState> getStatePredicate() {
});

public static final int MANA_COST = 500;
public static final int MANA_COST_OPENING = 200000;
public static final int MIN_REQUIRED_PYLONS = 2;
private static final String TAG_TICKS_OPEN = "ticksOpen";
private static final String TAG_TICKS_SINCE_LAST_ITEM = "ticksSinceLastItem";
private static final String TAG_STACK_COUNT = "stackCount";
private static final String TAG_STACK = "portalStack";
public static final String TAG_PORTAL_FLAG = "_elvenPortal";

private final List<ItemStack> stacksIn = new ArrayList<>();
private final List<BlockPos> cachedPylonPositions = new ArrayList<>();

public int ticksOpen = 0;
private int ticksSinceLastItem = 0;
Expand All @@ -126,16 +129,16 @@ public AlfheimPortalBlockEntity(BlockPos pos, BlockState state) {
}

public static void commonTick(Level level, BlockPos worldPosition, BlockState blockState, AlfheimPortalBlockEntity self) {
if (blockState.getValue(BotaniaStateProperties.ALFPORTAL_STATE) == AlfheimPortalState.OFF) {
AlfheimPortalState state = blockState.getValue(BotaniaStateProperties.ALFPORTAL_STATE);
if (state == AlfheimPortalState.OFF) {
self.ticksOpen = 0;
return;
}
AlfheimPortalState state = blockState.getValue(BotaniaStateProperties.ALFPORTAL_STATE);
AlfheimPortalState newState = self.getValidState();
AlfheimPortalState newState = self.getValidState(state);

self.ticksOpen++;

AABB aabb = self.getPortalAABB();
AABB aabb = self.getPortalAABB(state);
boolean open = self.ticksOpen > 60;
XplatAbstractions.INSTANCE.fireElvenPortalUpdateEvent(self, aabb, open, self.stacksIn);

Expand Down Expand Up @@ -222,48 +225,10 @@ private boolean validateItemUsage(ItemEntity entity) {
}

private void blockParticle(AlfheimPortalState state) {
double dh, dy;

// Pick one of the inner positions
switch (level.random.nextInt(9)) {
case 0 -> {
dh = 0;
dy = 1;
}
case 1 -> {
dh = 0;
dy = 2;
}
case 2 -> {
dh = 0;
dy = 3;
}
case 3 -> {
dh = -1;
dy = 1;
}
case 4 -> {
dh = -1;
dy = 2;
}
case 5 -> {
dh = -1;
dy = 3;
}
case 6 -> {
dh = 1;
dy = 1;
}
case 7 -> {
dh = 1;
dy = 2;
}
case 8 -> {
dh = 1;
dy = 3;
}
default -> throw new AssertionError();
}
// Pick one of the inner positions, offsets [-1,+1] and [+1,+3]
int rnd = level.random.nextInt(9);
double dh = (rnd / 3) - 1;
double dy = (rnd % 3) + 1;
double dx = state == AlfheimPortalState.ON_X ? 0 : dh;
double dz = state == AlfheimPortalState.ON_Z ? 0 : dh;

Expand All @@ -276,7 +241,7 @@ private void blockParticle(AlfheimPortalState state) {
public boolean onUsedByWand(@Nullable Player player, ItemStack stack, Direction side) {
AlfheimPortalState state = getBlockState().getValue(BotaniaStateProperties.ALFPORTAL_STATE);
if (state == AlfheimPortalState.OFF) {
AlfheimPortalState newState = getValidState();
AlfheimPortalState newState = getValidState(state);
if (newState != AlfheimPortalState.OFF) {
level.setBlockAndUpdate(getBlockPos(), getBlockState().setValue(BotaniaStateProperties.ALFPORTAL_STATE, newState));
if (player instanceof ServerPlayer serverPlayer) {
Expand All @@ -289,13 +254,10 @@ public boolean onUsedByWand(@Nullable Player player, ItemStack stack, Direction
return false;
}

private AABB getPortalAABB() {
AABB aabb = new AABB(worldPosition.offset(-1, 1, 0), worldPosition.offset(2, 4, 1));
if (getBlockState().getValue(BotaniaStateProperties.ALFPORTAL_STATE) == AlfheimPortalState.ON_X) {
aabb = new AABB(worldPosition.offset(0, 1, -1), worldPosition.offset(1, 4, 2));
}

return aabb;
private AABB getPortalAABB(AlfheimPortalState state) {
return state == AlfheimPortalState.ON_X
? new AABB(worldPosition.offset(0, 1, -1), worldPosition.offset(1, 4, 2))
: new AABB(worldPosition.offset(-1, 1, 0), worldPosition.offset(2, 4, 1));
}

private void addItem(ItemStack stack) {
Expand All @@ -315,7 +277,7 @@ public static Collection<ElvenTradeRecipe> elvenTradeRecipes(Level world) {
}

private void resolveRecipes() {
List<BlockPos> pylons = locatePylons();
List<BlockPos> pylons = locatePylons(true);
for (Recipe<?> r : BotaniaRecipeTypes.getRecipes(level, BotaniaRecipeTypes.ELVEN_TRADE_TYPE).values()) {
if (!(r instanceof ElvenTradeRecipe recipe)) {
continue;
Expand Down Expand Up @@ -381,8 +343,25 @@ public void readPacketNBT(CompoundTag cmp) {
ticksSinceLastItem = cmp.getInt(TAG_TICKS_SINCE_LAST_ITEM);
}

private AlfheimPortalState getValidState() {
Rotation rot = MULTIBLOCK.get().validate(level, getBlockPos());
private static Rotation getStateRotation(AlfheimPortalState state) {
return switch (state) {
case ON_X -> Rotation.CLOCKWISE_90;
case ON_Z -> Rotation.NONE;
default -> null;
};
}

private AlfheimPortalState getValidState(AlfheimPortalState oldState) {
Rotation rot;
if (oldState != AlfheimPortalState.OFF) {
Rotation oldRot = getStateRotation(oldState);
if (!MULTIBLOCK.get().validate(level, getBlockPos(), oldRot)) {
return AlfheimPortalState.OFF;
}
rot = oldRot;
} else {
rot = MULTIBLOCK.get().validate(level, getBlockPos());
}
if (rot == null) {
return AlfheimPortalState.OFF;
}
Expand All @@ -394,28 +373,50 @@ private AlfheimPortalState getValidState() {
};
}

public List<BlockPos> locatePylons() {
public List<BlockPos> locatePylons(boolean rescanNow) {
if (!rescanNow && cachedPylonPositions.size() >= MIN_REQUIRED_PYLONS) {
List<BlockPos> cachedResult = new ArrayList<>();
for (BlockPos pos : cachedPylonPositions) {
if (isValidPylonPosition(pos)) {
cachedResult.add(pos);
}
}
if (cachedResult.size() >= MIN_REQUIRED_PYLONS) {
return cachedResult;
}

// not enough valid cached pylons, scan again
}

int range = 5;
List<BlockPos> result = new ArrayList<>();

for (BlockPos pos : BlockPos.betweenClosed(getBlockPos().offset(-range, -range, -range),
getBlockPos().offset(range, range, range))) {
if (getLevel().hasChunkAt(pos)
&& getLevel().getBlockState(pos).is(BotaniaBlocks.naturaPylon)
&& getLevel().getBlockState(pos.below()).getBlock() instanceof ManaPoolBlock) {
if (isValidPylonPosition(pos)) {
result.add(pos.immutable());
}
}

cachedPylonPositions.clear();
cachedPylonPositions.addAll(result);

return result;
}

private boolean isValidPylonPosition(BlockPos pos) {
return getLevel().hasChunkAt(pos)
&& getLevel().getBlockState(pos).is(BotaniaBlocks.naturaPylon)
&& getLevel().getBlockState(pos.below()).getBlock() instanceof ManaPoolBlock;
}

public void lightPylons() {
if (ticksOpen < 50) {
return;
}

List<BlockPos> pylons = locatePylons();
boolean finishOpening = ticksOpen == 50;
List<BlockPos> pylons = locatePylons(finishOpening);
for (BlockPos pos : pylons) {
BlockEntity tile = level.getBlockEntity(pos);
if (tile instanceof PylonBlockEntity pylon) {
Expand All @@ -424,16 +425,16 @@ public void lightPylons() {
}
}

if (ticksOpen == 50) {
consumeMana(pylons, 200000, true);
if (finishOpening) {
consumeMana(pylons, MANA_COST_OPENING, true);
}
}

public boolean consumeMana(List<BlockPos> pylons, int totalCost, boolean close) {
List<ManaPoolBlockEntity> consumePools = new ArrayList<>();
int consumed = 0;

if (pylons.size() < 2) {
if (pylons.size() < MIN_REQUIRED_PYLONS) {
closeNow = true;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class ManaEnchanterBlockEntity extends BotaniaBlockEntity implements Mana
private static final String TAG_ITEM = "item";
private static final String TAG_ENCHANTS = "enchantsToApply";
private static final int CRAFT_EFFECT_EVENT = 0;
private static final int IDLE_CHECK_INTERVAL_TICKS = 10;

private static final String[][] PATTERN = new String[][] {
{
Expand Down Expand Up @@ -136,6 +137,8 @@ public class ManaEnchanterBlockEntity extends BotaniaBlockEntity implements Mana

public int stage3EndTicks = 0;

private int idleTicks = 0;

private int manaRequired = -1;
private int mana = 0;

Expand Down Expand Up @@ -256,26 +259,31 @@ public static void commonTick(Level level, BlockPos worldPosition, BlockState st
for (BlockPos offset : PYLON_LOCATIONS.get(axis)) {
BlockEntity tile = level.getBlockEntity(worldPosition.offset(offset));
if (tile instanceof PylonBlockEntity pylon) {
pylon.activated = self.stage == State.GATHER_MANA;
if (self.stage == State.GATHER_MANA) {
boolean gatheringMana = self.stage == State.GATHER_MANA;
pylon.activated = gatheringMana;
if (gatheringMana) {
pylon.centerPos = worldPosition;
}
}
}

if (self.stage != State.IDLE) {
self.stageTicks++;
} else {
self.idleTicks++;
}

if (level.isClientSide) {
if (level.isClientSide || self.stage == State.IDLE && self.idleTicks % IDLE_CHECK_INTERVAL_TICKS != 0) {
return;
}

if (FORMED_MULTIBLOCK.get().validate(level, worldPosition.below()) == null) {
Rotation rot = getAxisRotation(axis);
if (!FORMED_MULTIBLOCK.get().validate(level, worldPosition.below(), rot)) {
level.setBlockAndUpdate(worldPosition, Blocks.LAPIS_BLOCK.defaultBlockState());
XplatAbstractions.INSTANCE.sendToNear(level, worldPosition, new BotaniaEffectPacket(EffectType.ENCHANTER_DESTROY,
worldPosition.getX() + 0.5, worldPosition.getY() + 0.5, worldPosition.getZ() + 0.5));
level.playSound(null, worldPosition, BotaniaSounds.enchanterFade, SoundSource.BLOCKS, 1F, 1F);
return;
}

switch (self.stage) {
Expand Down Expand Up @@ -465,6 +473,14 @@ public static Direction.Axis canEnchanterExist(Level world, BlockPos pos) {
};
}

private static Rotation getAxisRotation(Direction.Axis axis) {
return switch (axis) {
case Z -> Rotation.NONE;
case X -> Rotation.CLOCKWISE_90;
default -> throw new IllegalStateException("Enchanter should only ever be facing in X or Z direction");
};
}

@Override
public boolean canAttachSpark(ItemStack stack) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
{
"type": "botania:mana_infusion",
"text": "botania.page.manaAlchemy5",
"recipes": [
"botania:mana_infusion/redstone_to_glowstone_dust",
"botania:mana_infusion/glowstone_deconstruct"
]
"recipes": "botania:mana_infusion/glowstone_deconstruct"
},
{
"type": "botania:mana_infusion",
Expand All @@ -55,7 +52,8 @@
"botania:mana_infusion/blaze_quartz_deconstruct",
"botania:mana_infusion/lavender_quartz_deconstruct",
"botania:mana_infusion/red_quartz_deconstruct",
"botania:mana_infusion/elf_quartz_deconstruct"
"botania:mana_infusion/elf_quartz_deconstruct",
"botania:mana_infusion/sunny_quartz_deconstruct"
]
},
{
Expand Down Expand Up @@ -92,9 +90,7 @@
{
"type": "botania:mana_infusion",
"text": "botania.page.manaAlchemy12",
"recipes": [
"botania:mana_infusion/potato_unpoison"
]
"recipes": "botania:mana_infusion/potato_unpoison"
},
{
"type": "botania:mana_infusion",
Expand All @@ -104,7 +100,10 @@
{
"type": "botania:mana_infusion",
"text": "botania.page.manaAlchemy14",
"recipes": "botania:mana_infusion/flint_to_gunpowder"
"recipes": [
"botania:mana_infusion/gunpowder_to_flint",
"botania:mana_infusion/flint_to_gunpowder"
]
},
{
"type": "botania:mana_infusion",
Expand All @@ -121,8 +120,8 @@
"heading": "botania.page.manaAlchemy35",
"text": "botania.page.manaAlchemy17",
"recipes": [
"botania:mana_infusion/cactus_to_slime",
"botania:mana_infusion/slime_to_cactus"
"botania:mana_infusion/slime_to_cactus",
"botania:mana_infusion/cactus_to_slime"
]
},
{
Expand All @@ -136,8 +135,7 @@
"text": "botania.page.manaAlchemy19",
"recipes": [
"botania:mana_infusion/glowstone_dust_to_redstone",
"botania:mana_infusion/redstone_to_glowstone_dust",
"botania:mana_infusion/glowstone_deconstruct"
"botania:mana_infusion/redstone_to_glowstone_dust"
]
},
{
Expand Down
4 changes: 3 additions & 1 deletion contributors.properties
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ swuff_=6
t_raw_=hiveacinth
taciturasa=6
taeeril=10
takenizzy=endoflame
takenizzy=shulk_me_not
talanist=loonium
tamaized=entropinnyum
tarawind=6
Expand Down Expand Up @@ -886,3 +886,5 @@ cookiegd__=spectrolus
themelon27=fallenkanade
guildes_=5
yymb=15
ravendarkeye=9
kairosylph=spectrolus

0 comments on commit 4a5fb68

Please sign in to comment.