Skip to content

Commit

Permalink
Refactors and a render fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityfox committed Nov 14, 2016
1 parent da0607c commit f870688
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,9 @@

public class CacheMap<K, V> extends HashMap<K, V> {
final private BiFunction<Object, Map<K, V>, V> callback;
final private boolean cache;

public CacheMap(BiFunction<Object, Map<K, V>, V> callback) {
this.callback = callback;
this.cache = false;
}

public CacheMap(BiFunction<Object, Map<K, V>, V> callback, boolean cache) {
this.callback = callback;
this.cache = cache;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of FoxCore, licensed under the MIT License (MIT).
*
* Copyright (c) gravityfox - https://gravityfox.net/
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package net.foxdenstudio.sponge.foxcore.common.util;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.function.BiFunction;

public class WeakCacheMap<K, V> extends WeakHashMap<K, V> {
final private BiFunction<Object, Map<K, V>, V> callback;

public WeakCacheMap(BiFunction<Object, Map<K, V>, V> callback) {
this.callback = callback;
}

@Override
public V get(Object key) {
if (containsKey(key)) {
return super.get(key);
} else {
return callback.apply(key, this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@

package net.foxdenstudio.sponge.foxcore.mod.render;

import com.flowpowered.math.vector.Vector2i;
import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector3i;

import static org.lwjgl.opengl.GL11.*;

public class Highlight implements IRenderable {

private static final double OFFSET = 0.005;
private static final double OFFSET = 0.01;
private static final int PERIOD = 2000;
public transient double distance;
boolean[][][] filled = new boolean[3][3][3];
Expand All @@ -58,22 +59,22 @@ public Highlight(Vector3i pos, Vector3f color, float phase) {
this.phase = phase;
}

public void render() {
public void render(Vector2i offset) {
final float alpha = (1f - ((System.currentTimeMillis()) % PERIOD) / (float) PERIOD + phase) % 1f;
glColor4f(color.getX(), color.getY(), color.getZ(), alpha);
drawBoxLines();
drawBoxLines(offset);
glColor4f(color.getX(), color.getY(), color.getZ(), alpha / 4 + 0.1f);
drawBoxFaces();
drawBoxFaces(offset);
}

public void drawBoxFaces() {
public void drawBoxFaces(Vector2i offset) {

final double x1 = pos.getX() - OFFSET;
final double x1 = pos.getX() + offset.getX() - OFFSET;
final double y1 = pos.getY() - OFFSET;
final double z1 = pos.getZ() - OFFSET;
final double x2 = pos.getX() + OFFSET + 1;
final double z1 = pos.getZ() + offset.getY() - OFFSET;
final double x2 = pos.getX() + offset.getX() + OFFSET + 1;
final double y2 = pos.getY() + OFFSET + 1;
final double z2 = pos.getZ() + OFFSET + 1;
final double z2 = pos.getZ() + offset.getY() + OFFSET + 1;

glBegin(GL_QUADS);
if (!filled[0][1][1]) {
Expand Down Expand Up @@ -121,13 +122,13 @@ public void drawBoxFaces() {
glEnd();
}

public void drawBoxLines() {
final double x1 = pos.getX() - OFFSET;
public void drawBoxLines(Vector2i offset) {
final double x1 = pos.getX() + offset.getX() - OFFSET;
final double y1 = pos.getY() - OFFSET;
final double z1 = pos.getZ() - OFFSET;
final double x2 = pos.getX() + OFFSET + 1;
final double z1 = pos.getZ() + offset.getY() - OFFSET;
final double x2 = pos.getX() + offset.getX() + OFFSET + 1;
final double y2 = pos.getY() + OFFSET + 1;
final double z2 = pos.getZ() + OFFSET + 1;
final double z2 = pos.getZ() + offset.getY() + OFFSET + 1;

glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package net.foxdenstudio.sponge.foxcore.mod.render;

import com.flowpowered.math.vector.Vector2i;
import net.minecraft.client.Minecraft;

import java.util.ArrayList;
Expand All @@ -42,7 +43,7 @@ public HighlightList(Minecraft mc) {
}

@Override
public void render() {
public void render(Vector2i offset) {
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT);

glDisable(GL_LIGHTING);
Expand All @@ -55,7 +56,7 @@ public void render() {

glLineWidth(2f);
try {
this.forEach(Highlight::render);
this.forEach(highlight -> highlight.render(offset));
} catch (ConcurrentModificationException ignored) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

package net.foxdenstudio.sponge.foxcore.mod.render;

import com.flowpowered.math.vector.Vector2i;

public interface IRenderable {

void render();
void render(Vector2i offset);

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@

package net.foxdenstudio.sponge.foxcore.mod.render;

import com.flowpowered.math.vector.Vector2i;
import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector3i;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -53,6 +56,7 @@ public RenderHandler(Minecraft mc) {
list = new HighlightList(mc);
}

@SideOnly(Side.CLIENT)
@SubscribeEvent
public void render(RenderWorldLastEvent event) {

Expand All @@ -66,9 +70,10 @@ public void render(RenderWorldLastEvent event) {

glPushMatrix();

glTranslated(-playerX, -playerY, -playerZ);
Vector2i offset = new Vector2i(-playerX, -playerZ);

list.render();
glTranslated(-playerX - offset.getX(), -playerY, -playerZ - offset.getY());
list.render(offset);
glPopMatrix();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private void registerPackets() {

private void registerWands() {
FCWandRegistry registry = FCWandRegistry.getInstance();
registry.registerBuilder(PositionWand.type, new PositionWand.Factory());
registry.registerBuilder(PositionWand.TYPE, new PositionWand.Factory());
registry.registerBuilder(CounterWand.type, new CounterWand.Factory());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ public boolean isEmpty() {
public ISelection getSelection() {
return currentSelection;
}

public void setCurrentSelection(ISelection currentSelection) {
this.currentSelection = currentSelection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
*/
public class PositionWand implements IWand {

public static final String type = "position";
public static final String TYPE = "position";

public static final DataQuery colorQuery = DataQuery.of("color");
public static final DataQuery rainbowQuery = DataQuery.of("rainbow");
public static final DataQuery COLOR_QUERY = DataQuery.of("color");
public static final DataQuery RAINBOW_QUERY = DataQuery.of("rainbow");

private Position.Color color;
private boolean rainbow;
Expand Down Expand Up @@ -134,7 +134,7 @@ private Text getColorText(){

@Override
public String type() {
return type;
return TYPE;
}

@Override
Expand All @@ -145,8 +145,8 @@ public int getContentVersion() {
@Override
public DataContainer toContainer() {
return new MemoryDataContainer()
.set(colorQuery, color.name())
.set(rainbowQuery, rainbow);
.set(COLOR_QUERY, color.name())
.set(RAINBOW_QUERY, rainbow);
}

public static class Factory implements IWandFactory {
Expand Down Expand Up @@ -197,14 +197,14 @@ public List<String> createSuggestions(String arguments, CommandSource source, @N

@Override
public IWand build(DataView dataView) {
if (dataView.contains(colorQuery) && dataView.contains(rainbowQuery))
return new PositionWand(Position.Color.from(dataView.getString(colorQuery).orElse(null)), dataView.getBoolean(rainbowQuery).orElse(false));
if (dataView.contains(COLOR_QUERY) && dataView.contains(RAINBOW_QUERY))
return new PositionWand(Position.Color.from(dataView.getString(COLOR_QUERY).orElse(null)), dataView.getBoolean(RAINBOW_QUERY).orElse(false));
else return new PositionWand();
}

@Override
public String type() {
return type;
return TYPE;
}

@Override
Expand Down

0 comments on commit f870688

Please sign in to comment.